3.设计用于浅串行化的类
2007-10-19 12:07
169 查看
3.1 从可串行化的类中定制XML串行化
1.格式化XML文档元素
[XmlRoot(ElementName = "Pupil", Namespace = "urn:MyNamespace")]
public class Student
[XmlElement(ElementName = "FullName", Namespace = "urn:OtherNamespace")]
public string Name
<?xml version="1.0" encoding="utf-8"?>
<Pupil xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:d1p1="urn:OtherNamespace" >
<d1p1:FullName>Thomas Smith</d1p1:FullName>
</Pupil>
这里的d1p1是自动生成的,在标题4,有办法自己指定Namespace前缀。
3.格式化XML属性
[XmlAttribute(AttributeName="StudentNumber", Namespace="urn:MyNamespace")]
public string Name
<?xml version="1.0" encoding="utf-8"?>
<Pupil xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:d1p1="urn:OtherNamespace"
d1p1:Name="Thomas Smith">
</Pupil>
XML属性在空间利用率上比XML元素略高一些。
4.为元素/属性设计限定的命名空间
使用XmlSerializer的Serialize方法重载,额外带一个XmlSerializerNamespace参数,指定这个命名空间前缀
public void SerializeIt(string filename)
<?xml version="1.0" encoding="utf-8"?>
<Pupil xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:otherNS="urn:OtherNamespace"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
otherNS:StudentNumber="8007" xmlns="urn:MyNamespace">
<otherNS:FullName>Thomas Smith</otherNS:FullName>
</Pupil>
5.格式化文本内容
[XmlText()]
public string Name
[XmlElement(DataType="date")]
public DateTime EnrollDate
public enum Color
private Color showColor;
public Color ShowColor
[XmlArray(ElementName="Cources"),
XmlArrayItem(Type=typeof(String), ElementName="CourceName"),
XmlArrayItem(Type=typeof(Int32), ElementName="CourceCode")]
public Object[] Subjects
<Cources>
<CourceName>Physics</CourceName>
<CourceCode>123</CourceCode>
<CourceName>IT</CourceName>
</Cources>
9.定义可空的对象引用
如果某属性为null,在串行化时会忽略该属性,可以显示替代的信息,方法如下:
[XmlElement(IsNullable = true)]
public string Address
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, "http://www.w3.org/2001/XMLSchema");
writer.WriteAttributeString("xmlns", "xsi", Nothing, "http://www.w3.org/2001/XMLSchema-instance");
writer.WriteAttributeString("xmlns", "soap", Nothing, "http://schemas.xmlsoap.org/soap/encoding/");
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,其它的对应如下:
1.格式化XML文档元素
[XmlRoot(ElementName = "Pupil", Namespace = "urn:MyNamespace")]
public class Student
[XmlElement(ElementName = "FullName", Namespace = "urn:OtherNamespace")]
public string Name
<?xml version="1.0" encoding="utf-8"?>
<Pupil xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:d1p1="urn:OtherNamespace" >
<d1p1:FullName>Thomas Smith</d1p1:FullName>
</Pupil>
这里的d1p1是自动生成的,在标题4,有办法自己指定Namespace前缀。
3.格式化XML属性
[XmlAttribute(AttributeName="StudentNumber", Namespace="urn:MyNamespace")]
public string Name
<?xml version="1.0" encoding="utf-8"?>
<Pupil xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:d1p1="urn:OtherNamespace"
d1p1:Name="Thomas Smith">
</Pupil>
XML属性在空间利用率上比XML元素略高一些。
4.为元素/属性设计限定的命名空间
使用XmlSerializer的Serialize方法重载,额外带一个XmlSerializerNamespace参数,指定这个命名空间前缀
public void SerializeIt(string filename)
<?xml version="1.0" encoding="utf-8"?>
<Pupil xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:otherNS="urn:OtherNamespace"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
otherNS:StudentNumber="8007" xmlns="urn:MyNamespace">
<otherNS:FullName>Thomas Smith</otherNS:FullName>
</Pupil>
5.格式化文本内容
[XmlText()]
public string Name
[XmlElement(DataType="date")]
public DateTime EnrollDate
public enum Color
private Color showColor;
public Color ShowColor
[XmlArray(ElementName="Cources"),
XmlArrayItem(Type=typeof(String), ElementName="CourceName"),
XmlArrayItem(Type=typeof(Int32), ElementName="CourceCode")]
public Object[] Subjects
<Cources>
<CourceName>Physics</CourceName>
<CourceCode>123</CourceCode>
<CourceName>IT</CourceName>
</Cources>
9.定义可空的对象引用
如果某属性为null,在串行化时会忽略该属性,可以显示替代的信息,方法如下:
[XmlElement(IsNullable = true)]
public string Address
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, "http://www.w3.org/2001/XMLSchema");
writer.WriteAttributeString("xmlns", "xsi", Nothing, "http://www.w3.org/2001/XMLSchema-instance");
writer.WriteAttributeString("xmlns", "soap", Nothing, "http://schemas.xmlsoap.org/soap/encoding/");
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 |
相关文章推荐
- 设计用于浅串行化的类
- 设计用于浅串行化的类
- MVC是一种用于表示层设计的复合设计模式
- Origami – 用于 Quartz 的免费的交互设计框架
- Bochs简介-用于GeekOS课程设计
- 10个用于设计网页线框图的优秀工具
- 相邻颜色搭配,可用于表格设计
- 设计一个抽象类CSolid,含有用于求表面积及体积的两个纯虚函数。设计派生类CCube、CBall、CCylinder,分别表示正方体、球体及圆柱体。在main()函数中,定义CSolid *p;(p
- MAX1325636V H桥变压器驱动器,用于隔离电源设计
- MOSS 2010:如何为列表设计Infopath表单用于编辑和显示
- 分享30款用于极简设计的英文圆滑字体
- 4用于cifar10的卷积神经网络-4.10设计实验观察不同的优化器在不同的学习率下的网络性能
- 第八周 上机练习 设计一个系统,用于赛车上的计算机系统
- 4用于cifar10的卷积神经网络-4.2/4.3cifar10卷积神经网络的计算图设计(上下)
- 用于并行计算的多线程数据结构,第 1 部分: 设计并发数据结构
- 触摸屏显示接口设计-用于便携式热像仪的OMAP双核处理器
- 创建Servlet基类,用于解决由于业务太多,来编写太多是Servlet类。--模板设计模式
- 用于响应式设计的9个CSS技巧
- 用于响应式设计的9个CSS技巧
- 用于响应式设计的9个CSS技巧