5.格式化文本內容
public string Name
{
get { return name; }
set { name = value; }
}
則Name會生成在<Pupil>XXX(Name)</Pupil>中作爲文本
6.爲元素/屬性定義數據類型
XML Schema類型與.NET數據類型有一個Mapping,比如說type對應System.DateTime
public DateTime EnrollDate
{
get { return enrollDate; }
set { enrollDate = value; }
}
於是生成XML: <EnrollDate>2007-10-19</EnrollDate>
XMLRoot,XmlAttribute,XMLElement,XMLText,XMLArrayItem標籤都可以指定DataType
7.爲枚舉修飾符指定其他名稱
{
[XmlEnum(Name = " White Color " )]
White,
[XmlEnum(Name = " Black Color " )]
Black,
[XmlEnum(Name = " Red Color " )]
Red
}
private Color showColor;
public Color ShowColor
{
get { return showColor; }
set { showColor = value; }
}
生成XML如下格式:
<ShowColor>White Color</ShowColor>
8.串行化多肽數組
XmlArrayItem(Type = typeof (String), ElementName = " CourceName " ),
XmlArrayItem(Type = typeof (Int32), ElementName = " CourceCode " )]
public Object[] Subjects
{
get { return subjects; }
set { subjects = value; }
}
XmlArrayItem負責指定數組中可能出現的元素類型,以及該類型對應的XML前綴
比如說創建如下的數組:
Object obj = new Object["Physics", 123, "IT"];
生成XML如下格式:
< CourceName > Physics </ CourceName >
< CourceCode > 123 </ CourceCode >
< CourceName > IT </ CourceName >
</ Cources >
9.定義可空的對象引用
如果某屬性爲null,在串行化時會忽略該屬性,可以顯示替代的信息,方法如下:
public string Address
{
get { return address; }
set { address = value; }
}
在該位置,生成替代XML:
<Address xsi:nil="true" />
10.定義可忽略的字段/屬性
[XmlIgnore()]