Spot the web RSS 2.0
# Wednesday, November 28, 2007

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...

Wednesday, November 28, 2007 9:30:33 AM (Jerusalem Standard Time, UTC+02:00)  #    Comments [0] - Trackback
Schema | Serialization | XML | XSD
Navigation
Archive
<March 2010>
SunMonTueWedThuFriSat
28123456
78910111213
14151617181920
21222324252627
28293031123
45678910
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2010
Guy Levin
Sign In
Statistics
Total Posts: 63
This Year: 0
This Month: 0
This Week: 0
Comments: 14
Themes
All Content © 2010, Guy Levin