oop - How to assign Mandatory Properties in class object in C# -
im working on .net using c#. in c# classes want use optional properties, have person class, in have properties name,age,city , gender.
public class person { public string name{get; set;}; public int age {get; set;} public string city {get; set;} public string gender {get; set;} }
now assigned property values name,age , city.
person objper = new person(); objper.name="abc"; objper.age="22"; objper.city="nyc"; string strresult = calperson(objper);
now im passing person object 'objper' 1 method, returns property values of person in xml format below.im not passing value gender property.
public string calperson(person objperson) { //"<xml><name>abc</name><age>22</age><city>nyc</city><gender></gender></xml>" return xml formated string. }
while creating xml formatted string im getting gender node null value though not sending value it.
i want
<xml> <name>abc</name><age>22</age><city>nyc</city> </xml>
as output.
now have method calgender() accepts person object.
objper = new person(); objper.gender="male"; public string calgender(person objperson) { //"<xml><name></name><age></age><city></city><gender>male</gender></xml>" return xml formated string. }
while creating xml formatted string im getting name,age , city nodes value null though not sending value it.
i want
<xml> <gender>male</gender> </xml>
as output.
so, want use name,age , city properties calperson() method only. not want gender property in person class. want use gender property calgender() method only.i not want name,age , city properties in person class.
please suggest me how this...
aniljayanti.
you can use built in serializer datacontractserializer
or xmlserializer
, use [xmlignore]
attribute skip properties not want serialize.
for more details, refer here
Comments
Post a Comment