Russ
Russ

Reputation: 710

Best Practice for Serializing an XML Document from a Class

The current solution that I have is working fine. I have a C# class Person which I am serializing into an XML document using the XmlSerializer object. I know that the following class structure will be Serialized into the XML document (also below).

public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int Age { get; set; }
    [XmlAttribute("recordId")]
    public int recordId { get; set; }
}

... will be Serialized into the following XML:

<?xml version="1.0" encoding="utf-8"?>
<Person recordId="1">
  <FirstName>John</FirstName>
  <LastName>Doe</LastName>
  <Age>32</Age>
</Person>

Assume each property has the sameShould I explicitly preface each element appropriately with XmlElement tag as I have with the XmlAttribute; or is leaving the Person class as defined above perfectly fine and acceptable?

What are the implications for doing so or not doing so?

[XmlRoot("Person")]
public class Person
{
    [XmlElement("FirstName")]
    public string FirstName { get; set; }
    [XmlElement("LastName")]
    public string LastName { get; set; }
    [XmlElement("Age")]
    public int Age { get; set; }
    [XmlAttribute("recordId")]
    public int recordId { get; set; }
}

Upvotes: 0

Views: 1261

Answers (3)

Ron Beyer
Ron Beyer

Reputation: 11273

By default the XmlSerializer uses the property name for the XML element name and 99% of the time this works. The edge cases are where you need to have a custom element name mapped to a property name (for example, you are making the code model from the XML and the XML element begins with an invalid character).

Explicitly decorating them, to me, is just code bloat. If the default names are correct then let the XmlSerializer do its default actions with the names. There are very few (frowned upon) cases where explicitly decorating the name would let you change the underlying property name without breaking serialization, but if you had to do that then you can add the attribute at that time.

So for what its worth, I'd leave them off as long as the results are appropriate and only add them to signify that something is different with that particular property. For example, you gave the code:

public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int Age { get; set; }
    [XmlAttribute("recordId")]
    public int recordId { get; set; }
}

Which immediately draws the eye to recordId as being different. If you have the attributes explicitly set on all the items, it is less apparent that it has to be serialized differently.

Upvotes: 1

martavoi
martavoi

Reputation: 7092

Basically, it doesn't matter how to deal with it. But, as for me, it looks more readable when I have explicit XML serialization contract - an appropriate type for serializing without attributes. However, sometimes XML document requires elements with a names that are not suitable with a C# lang. naming specification and you have to use attributes :)

Upvotes: 0

Randy
Randy

Reputation: 2358

The XmlRoot attribute would allow you to control the name of the element without changing the name of the class.

If the class name is the same as the XML element name then there is no need to include it.

Upvotes: 0

Related Questions