Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between using XMLRoot/XMLElement and using Serializable() attributes (in c#)

what is Difference between using XMLRoot/XMLElement and using Serializable() attributes? how do i know when to use each ?

like image 821
Rodniko Avatar asked Nov 20 '10 07:11

Rodniko


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>[email protected]</email>
  </owner>
  <contact firstName="Natasha" lastName="Taylor">
    <tel1>555-321343</tel1>
    <email>[email protected]</email>
  </contact>
  <contact firstName="Gideon" lastName="Becking">
    <tel1>555-123423</tel1>
    <email>[email protected]</email>
  </contact>
</addressBook>
like image 176
Chris Taylor Avatar answered Sep 20 '22 07:09

Chris Taylor