First let's go over the basics:
By default, an XML element name is determined by the class or member name. In a simple class named Book, a field named Author will produce an XML element tag <Author>, as shown here:
public class Book
{
public string Author;
}
When an instance of the Book class is serialized, it might produce this XML:
....
<Author>John Doe</Author>
....
Now, here are some of the elements we can use to control the serialization:
[DefaultValueAttribute(0)]
Can only be applied to numeric members (int, float, double), when no value is given it will put the default value 0. It is very important to remember that if the value of this member will be 0 (or equals to the default value) this member will not be serialized!
public class Book
{
public string Author;
[DefaultValueAttribute(0)]
public int BookNum;
}
In the XSD Schema it will do this:
<xs:element minOccurs="0" maxOccurs="1" default="0" name="BookNum" type="xs:int" />
[XmlElement(ElementName = "Book_Number")]
With this attribute you can specify the element name instead of using the member name.
public class Book
{
public string Author;
[DefaultValueAttribute(0)]
[XmlElement(ElementName = "Book_Number")]
public int BookNum;
}
In the XSD Schema it will do this:
<xs:element minOccurs="0" maxOccurs="1" default="0" name="Book_Number" type="xs:int" />
Will be continued in Part 2...