I do a de-serialization of a XML file which is something like:
<?xml version="1.1" ?>
<Veggies>
<Carrot>
<quantity> 1 </quantity>
</Carrot>
</Veggies>
Ok. I write classes like
[Serializable]
public class Veggies
{
[XmlRoot("Veggies")]
public Carrot carrot;
}
[Serializable]
public class Carrot
{
[XmlElement("Quantity")]
public string Quantity;
}
All is well.
But if someone parses an input file which is something like below,
<?xml version="1.1" ?>
<Figs>
<quantity> 11 </quantity>
</Figs>
<Veggies>
<Carrot>
<quantity> 1 </quantity>
</Carrot>
</Veggies>
I get an error.
Could you please suggest me a solution so that the Quantity of carrot can alone be retrieved leaving the other data but throwing no errors ?
Thanks.
Ok guyz.. I figured myself out!
is not allowed because it does not respect the rule that “All Elements should be inside the RootElement tag.”
If the file had been something like,
Then the quantity of the carrot can alone be read and others don’t exactly throw any errors.
So, “Always put the elements inside a rootElement (here in example, “Veggies”)” or in other words, the elements should obey the de-serialization class structures.
Thanks.