Reputation: 141
Im working on .Net using c#. In c# classes want to use optional properties, like i have PERSON class, in that have properties like NAME,AGE,CITY and 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 i assigned the property values for NAME,AGE AND CITY.
PERSON objper = new PERSON();
objper.NAME="ABC";
objper.AGE="22";
objper.CITY="NYC";
string strresult = calPerson(objper);
now im passing this PERSON object 'objper' to one method, which returns the property values of PERSON in xml format like below.Im not passing the value for 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 with null value even though not sending any value to it.
i want only
<xml>
<name>abc</name><age>22</age><city>nyc</city>
</xml>
as output.
Now i have another method calGender() which 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 and city nodes value with null even though not sending any value to it.
i want only
<xml>
<gender>male</gender>
</xml>
as output.
so, i want to use NAME,AGE AND CITY properties to calPerson() method only. I do not want GENDER property in the PERSON class. i want to use GENDER property to calGender() method only.i do not want NAME,AGE and CITY properties in PERSON class.
Please suggest me how to do this...
AnilJayanti.
Upvotes: 0
Views: 1165
Reputation: 22011
Are you using the built in XML serialiser? If so, try creating some additional PERSON
properties with the same names, but adding the suffix Specified
, for example:
[XmlIgnore]
public bool GENDERSpecified { get { return !String.IsNullOrEmpty(GENDER); } }
This should instruct the XMLSerialiser to only output the GENDER node if it has a value.
Full details here: http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer.aspx
Upvotes: 1
Reputation: 91
public class PERSON
{
public string NAME{get; set;}
public int AGE {get; set;}
public string CITY {get; set;}
}
class PERSON1 : PERSON
{
public string GENDER {get; set;}
}
Use PERSON in calling "calPerson()' and PERSON1 in "calGender()"
Upvotes: 0