what is Difference between using XMLRoot/XMLElement and using Serializable() attributes? how do i know when to use each ?
1 Answers
Here is a less than in depth description, but I think a good starting point.
XmlRootAttribute
- Is used to provide schema information for the class that is going to be the root element of the object graph being serialized. This can only be applied to classes, structs, enums, interfaces of return values.
XmlElementAttribute
- Provides schema information for properties of a class controlling how they are serialized as child elements. This attribute can only be applied to fields (class variable members), properties, parameters and return values.
The first two XmlRootAttribute
and XmlElementAttribute
relate to the XmlSerializer.
While the next, is used by the runtime formatters and does not apply when using XmlSerialization.
SerializableAtttrible
- Is used to indicate that the type can be serialized by the runtime formatters like SoapFormatter or BinaryFormatter. This is only required if you need to serialize the type using one of the formatters and can be applied to delegates, enums, structs and classes.
Here is a quick example that might help clarify the above.
// This is the root of the address book data graph
// but we want root written out using camel casing
// so we use XmlRoot to instruct the XmlSerializer
// to use the name 'addressBook' when reading/writing
// the XML data
[XmlRoot("addressBook")]
public class AddressBook
{
// In this case a contact will represent the owner
// of the address book. So we deciced to instruct
// the serializer to write the contact details out
// as <owner>
[XmlElement("owner")]
public Contact Owner;
// Here we apply XmlElement to an array which will
// instruct the XmlSerializer to read/write the array
// items as direct child elements of the addressBook
// element. Each element will be in the form of
// <contact ... />
[XmlElement("contact")]
public Contact[] Contacts;
}
public class Contact
{
// Here we instruct the serializer to treat FirstName
// as an xml element attribute rather than an element.
// We also provide an alternate name for the attribute.
[XmlAttribute("firstName")]
public string FirstName;
[XmlAttribute("lastName")]
public string LastName;
[XmlElement("tel1")]
public string PhoneNumber;
[XmlElement("email")]
public string EmailAddress;
}
Given the above, an instance of AddressBook serialized with an XmlSerializer would give XML of the following format
<addressBook>
<owner firstName="Chris" lastName="Taylor">
<tel1>555-321343</tel1>
<email>chris@guesswhere.com</email>
</owner>
<contact firstName="Natasha" lastName="Taylor">
<tel1>555-321343</tel1>
<email>natasha@guesswhere.com</email>
</contact>
<contact firstName="Gideon" lastName="Becking">
<tel1>555-123423</tel1>
<email>gideon@guesswhere.com</email>
</contact>
</addressBook>

- 52,623
- 10
- 78
- 89
-
Can you tell how we can add the Attribiutes for an xml that has a tag
for all the contacts, inside the addressbook tag? – Dhanashree Dec 19 '16 at 10:32 -
3@Dhanashree, are you asking to wrap the contact elements in a parent contacts element? If so then you can remove [XmlElement("contact")] from the 'Contact[] contacts', if you want to control the names then you can replace the current attribute with [XmlArray("contacts")], XmlArrayItem("contact")] that will control the name of the root and the items in the collection. – Chris Taylor Dec 19 '16 at 16:46
-
Thanks! That helped :) – Dhanashree Dec 20 '16 at 13:20