Deselect xml to object c # (xml does not always have all nodes)

2

I have to realize an XML to an object of a defined class, the problem is the following: assuming the class has 3 attributes (a, b and c), the xml come with the 3 attributes and it is easy to realize them to the object, my inconvenience is that sometimes the XML does not come with all the complete nodes, to say only comes with the node a and c, then how do I do it to an object and for the node that does not have to put null for example or another default value.

    
asked by RSillerico 27.10.2016 в 15:41
source

2 answers

1

You can define the property IsNullable in the XmlElementAttribute defined. For example:

public class MyClass
{
   [XmlElement(IsNullable = true)]
   public string Campo1;
   [XmlElement(IsNullable = true)]
   public string Campo2;
}
    
answered by 31.10.2016 в 17:50
-1

If you have not been able to solve it using the attributes mentioned above. I would use the DataContract and DataMember attributes and create my own deserializer.

But, if what you are is consuming an xml of some external resource instead of serializing it directly you can get the answer and manually with a help method assign the values and you can establish values for when any of the values come up.

PS: If you want to load an xml and load its values into an object then use linq to XML: MSDN Linq to XML

    
answered by 31.10.2016 в 15:12