0

I have a class - say Place

public class Place{
    public int id;
    public string name;
    public string slug;
}

There are other classes like City, State, Country which are derived from Place class.

I get an XML string like this, that has array of cities

<cities>
 <City>
  <name>
   <![CDATA[Mumbai]]>
  </name>
  <slug>
   <![CDATA[mumbai]]>
  </slug>
 </City>
<City>
  <name>
   <![CDATA[Thane]]>
  </name>
  <slug>
   <![CDATA[thane]]>
  </slug>
 </City>
</cities>

I deserialize this string in the city array and send to Angular UI.

On the frontend, there is no use of id field. But since it is part of class, the empty/null id field also gets deserialize. I can remove the id field from the class, but it is used in other functions so I cannot do that. I don't want to use XmlIgnore attribute because some one might have already used this field in other functions.

My question is, is there a way to remove the id field before sending it forward?

return Serializer.DeserializeXml<Cities>(_strCities);
Dhanashree
  • 235
  • 1
  • 3
  • 20

2 Answers2

0

You can use [XmlIgnore] and [ScriptIgnore] attributes above property

[XmlIgnore]
[ScriptIgnore]
public int intvar{get;set;}
kgzdev
  • 2,770
  • 2
  • 18
  • 35
0

The solution was quite simple. I had to add another function - ShouldSerializePropertyName. The function will return true or false and depending on the result the id will be serialized or deserialized :)

public bool ShouldSerializeid()
        {
            return !(id.Equals(string.Empty)|| id.Equals(0));
        }

I found this explanation on MSDN -

ShouldSerialize and Reset are optional methods that you can provide for a property, if the property does not a have simple default value. If the property has a simple default value, you should apply the DefaultValueAttribute and supply the default value to the attribute class constructor instead.

Dhanashree
  • 235
  • 1
  • 3
  • 20