C# - XML Serialization
Creating XML for C# is easy. Reading in an externally created XML into C# is not complicated.
You can use C# XmlSerializer.Deserialize() to read in and deserialize the XML file. But often, the externally created XML file does not have the same root or element names as per C# default naming value.
To make it work with custom XML tag name, all you need is to have a serialization tag to your C# object class. Below is a sample to read in customized root, element and attribute name.
[Serializable]
[XmlRoot(ElementName = "my-info")]
public class MyInfo
{
[XmlAttribute(AttributeName="my-attribute")]
public string myAttribute;
[XmlElement(ElementName = "my-element")]
public string myElement;
[XmlArray(ElementName="my-array")]
[XmlArrayItem(ElementName="my-item")]
[XmlArrayItem(ElementName="my-item")]
public string [] my-array;
}
Comments
Post a Comment