分类:
2010-06-23 16:18:45
[XmlRoot(ElementName = "Pupil", Namespace = "urn:MyNamespace")]
public class Student
{
//则生成XML文档中,根元素
}
[XmlElement(ElementName = "FullName", Namespace = "urn:OtherNamespace")]
public string Name
{
get
{ return name; }
set
{ name = value; }
}
xml version="1.0" encoding="utf-8"?>
<Pupil xmlns:xsd=""
xmlns:d1p1="urn:OtherNamespace" >
<d1p1:FullName>Thomas Smithd1p1:FullName>
Pupil>
[XmlAttribute(AttributeName="StudentNumber", Namespace="urn:MyNamespace")]
public string Name
{
get
{ return name; }
set
{ name = value; }
}
xml version="1.0" encoding="utf-8"?>
<Pupil xmlns:xsd=""
xmlns:d1p1="urn:OtherNamespace"
d1p1:Name="Thomas Smith">
Pupil>
public void SerializeIt(string filename)
{
//自定义命名空间
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("xsd", "");
ns.Add("xsi", "-instance");
ns.Add("otherNS", "urn:OtherNamespace");
XmlSerializer serializer = new XmlSerializer(typeof(Book));
StreamWriter writer = new StreamWriter(filename);
Book myBook = new Book();
//使用Serialize重载方法
serializer.Serialize(writer, myBook, ns);
writer.Close();
}
xml version="1.0" encoding="utf-8"?>
<Pupil xmlns:xsd=""
xmlns:otherNS="urn:OtherNamespace"
xmlns:xsi="-instance"
otherNS:StudentNumber="8007" xmlns="urn:MyNamespace">
<otherNS:FullName>Thomas SmithotherNS:FullName>
Pupil>
[XmlText()]
public string Name
{
get
{ return name; }
set
{ name = value; }
}
[XmlElement(DataType="date")]
public DateTime EnrollDate
{
get
{ return enrollDate; }
set
{ enrollDate = value; }
}
public enum Color
{
[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; }
}
[XmlArray(ElementName="Cources"),
XmlArrayItem(Type=typeof(String), ElementName="CourceName"),
XmlArrayItem(Type=typeof(Int32), ElementName="CourceCode")]
public Object[] Subjects
{
get
{ return subjects; }
set
{ subjects = value; }
}
<Cources>
<CourceName>PhysicsCourceName>
<CourceCode>123CourceCode>
<CourceName>ITCourceName>
Cources>
[XmlElement(IsNullable = true)]
public string Address
{
get
{ return address; }
set
{ address = value; }
}在该位置,生成替代XML:
10.定义可忽略的字段/属性
[XmlIgnore()]
3.2 把XML串行化定制为SOAP编码格式
public void MySerialize(Student obj, string filename)
{
SoapReflectionImporter importer = new SoapReflectionImporter();
XmlTypeMapping mapping = importer.ImportMembersMapping(typeof(Student));
XmlTextWriter writer = new XmlTextWriter(filename, System.Text.Encoding.UTF8);
writer.WriteStartElement("MyWrapperElement");
writer.WriteAttributeString("xmlns", "xsd", Nothing, "");
writer.WriteAttributeString("xmlns", "xsi", Nothing, "-instance");
writer.WriteAttributeString("xmlns", "soap", Nothing, "");
writer.WriteAttributeString("xmlns", "otherNS", Nothing, "urn:OtherNamespace");
XmlSerializer serializer = new XmlSerializer(mapping);
serializer.Serialize(writer, obj);
writer.WriteEndElement();
writer.Close();
}于是,生产SOAP格式的XML文件。
Soap编码串行化的属性,无XMLText和XMLArray,其它的对应如下:
| SoapType | XmlType |
| SoapElement | XmlElement |
| SoapAttribute | XmlAttribute |
| SoapEnum | XmlEnum |
| SoapIgnore | XmlIgnore |
| SoapInclude | XmlInclude |