Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add a attribute to a XmlArray element (XML Serialization)?

How do I add a attribute to a XmlArray element ( not to XmlArrayItem ) while serializing the object?

like image 533
123Developer Avatar asked Jun 27 '09 10:06

123Developer


People also ask

Which of the following attribute controls XML serialization of the attribute target as an XML root element?

XmlRootAttribute Class (System.Xml.Serialization) Controls XML serialization of the attribute target as an XML root element.

What is the correct way of using XML serialization?

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.

What is an XML element attribute vs element?

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.

What is XML serialization and what is the purpose of using it?

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.


1 Answers

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> 
like image 135
Ray Lu Avatar answered Oct 01 '22 17:10

Ray Lu