How do I add a attribute to a XmlArray element ( not to XmlArrayItem ) while serializing the object?
XmlRootAttribute Class (System.Xml.Serialization) Controls XML serialization of the attribute target as an XML root element.
As with the CreatePo method, you must first construct an XmlSerializer, passing the type of the class to be deserialized to the constructor. Also, a FileStream is required to read the XML document. To deserialize the objects, call the Deserialize method with the FileStream as an argument.
An element can have multiple unique attributes. Attribute gives more information about XML elements. To be more precise, they define properties of elements. An XML attribute is always a name-value pair.
XML serialization is the process of converting XML data from its representation in the XQuery and XPath data model, which is the hierarchical format it has in a Db2® database, to the serialized string format that it has in an application.
XmlArray is used to tell the xmlserializer to treat the property as array and serialize it according its parameters for the element names.
[XmlArray("FullNames")] [XmlArrayItem("Name")] public string[] Names{get;set;}
will give you
<FullNames> <Name>Michael Jackson</Name> <Name>Paris Hilton</Name> </FullNames>
In order to add an xml attribute to FullNames element, you need declare a class for it.
[XmlType("FullNames")] public class Names { [XmlAttribute("total")] public int Total {get;set;} [XmlElement("Name")] public string[] Names{get;set;} }
This will give you
<FullNames total="2"> <Name>Michael Jackson</Name> <Name>Paris Hilton</Name> </FullNames>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With