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

1

16

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

Schoenfeld answered 20/11, 2010 at 7:53 Comment(0)
A
38

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>
Aishaaisle answered 20/11, 2010 at 8:0 Comment(3)
Can you tell how we can add the Attribiutes for an xml that has a tag <contacts/> for all the contacts, inside the addressbook tag?Retinite
@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.Aishaaisle
Thanks! That helped :)Retinite

© 2022 - 2024 — McMap. All rights reserved.