如何将对象读写到 XML 中
来源:互联网 发布:汉字转换拼音软件 编辑:程序博客网 时间:2024/05/03 23:03
- 创建可序列化的类,或者创建用于处理 XML 序列化类的类。 如果您拥有描述要加载或保存的 XML 文件格式的 XML 架构定义 (XSD),请使用 Xsd.exe 工具自动创建这些类。 还可以手动创建这些类。 若要使用 Xsd.exe 工具创建可序列化的类,请按照下列步骤操作:
- 将以下描述订单的 XSD 架构保存为 Po.xsd 文件:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" targetNamespace="" > <xsd:annotation> <xsd:documentation> Purchase order schema for Example.com. Copyright 2000 Example.com. All rights reserved. </xsd:documentation> </xsd:annotation> <xsd:element name="purchaseOrder" type="PurchaseOrder" /> <xsd:element name="comment" type="xsd:string"/> <xsd:complexType name="PurchaseOrder"> <xsd:sequence> <xsd:element name="shipTo" type="USAddress"/> <xsd:element name="billTo" type="USAddress"/> <xsd:element ref="comment" minOccurs="0"/> <xsd:element name="items" type="Items"/> </xsd:sequence> <xsd:attribute name="orderDate" type="xsd:date"/> </xsd:complexType> <xsd:complexType name="USAddress"> <xsd:sequence> <xsd:element name="name" type="xsd:string"/> <xsd:element name="street" type="xsd:string"/> <xsd:element name="city" type="xsd:string"/> <xsd:element name="state" type="xsd:string"/> <xsd:element name="zip" type="xsd:decimal"/> </xsd:sequence> <xsd:attribute name="country" type="xsd:NMTOKEN" /> </xsd:complexType> <xsd:complexType name="Items"> <xsd:sequence> <xsd:element name="item" minOccurs="0" maxOccurs="unbounded"> <xsd:complexType> <xsd:sequence> <xsd:element name="productName" type="xsd:string"/> <xsd:element name="quantity"> <xsd:simpleType> <xsd:restriction base="xsd:positiveInteger"> <xsd:maxExclusive value="100"/> </xsd:restriction> </xsd:simpleType> </xsd:element> <xsd:element name="USPrice" type="xsd:decimal"/> <xsd:element ref="comment" minOccurs="0"/> <xsd:element name="shipDate" type="xsd:date" minOccurs="0"/> </xsd:sequence> <xsd:attribute name="partNum" type="SKU"/> </xsd:complexType> </xsd:element> </xsd:sequence> </xsd:complexType> <xsd:simpleType name="SKU"> <xsd:restriction base="xsd:string"> <xsd:pattern value="/d{3}-[A-Z]{2}"/> </xsd:restriction> </xsd:simpleType></xsd:schema>
- 使用 Xsd.exe 工具生成类文件。 例如,以下命令行代码生成 C# 类:Xsd.exe 工具在和上面给出的架构一起使用时为 Visual Basic .Net 生成以下类:
xsd /classes /language:C# PO.xsd
请注意,Xsd.exe 并不使用类插入名称空间名称。 用户必须根据应用程序设计手动完成此操作。 在此示例中,XmlSerializationHowTo 名称空间用于包装 Xsd.exe 生成的类。Imports System.Xml.SerializationNamespace XmlSerializationHowTo <System.Xml.Serialization.XmlRootAttribute("purchaseOrder", [Namespace]:="", IsNullable:=false)> _ Public Class PurchaseOrder <System.Xml.Serialization.XmlElementAttribute(IsNullable:=false)> _ Public shipTo As USAddress <System.Xml.Serialization.XmlElementAttribute(IsNullable:=false)> _ Public billTo As USAddress <System.Xml.Serialization.XmlElementAttribute(IsNullable:=false)> _ Public comment As String <System.Xml.Serialization.XmlArrayAttribute(IsNullable:=false), _ System.Xml.Serialization.XmlArrayItemAttribute("item", IsNullable:=false)> _ Public items() As ItemsItem <System.Xml.Serialization.XmlAttributeAttribute(DataType:="date")> _ Public orderDate As Date <System.Xml.Serialization.XmlIgnoreAttribute()> _ Public orderDateSpecified As BooleanEnd Class Public Class USAddress <System.Xml.Serialization.XmlElementAttribute(IsNullable:=false)> _ Public name As String <System.Xml.Serialization.XmlElementAttribute(IsNullable:=false)> _ Public street As String <System.Xml.Serialization.XmlElementAttribute(IsNullable:=false)> _ Public city As String <System.Xml.Serialization.XmlElementAttribute(IsNullable:=false)> _ Public state As String Public zip As Decimal <System.Xml.Serialization.XmlAttributeAttribute(DataType:="NMTOKEN")> _ Public country As StringEnd Class <System.Xml.Serialization.XmlTypeAttribute([Namespace]:="")> _ Public Class ItemsItem <System.Xml.Serialization.XmlElementAttribute(IsNullable:=false)> _ Public productName As String <System.Xml.Serialization.XmlElementAttribute(DataType:="positiveInteger", IsNullable:=false)> _ Public quantity As String Public USPrice As Decimal <System.Xml.Serialization.XmlElementAttribute(IsNullable:=false)> _ Public comment As String <System.Xml.Serialization.XmlElementAttribute(DataType:="date", IsNullable:=false)> _ Public shipDate As Date <System.Xml.Serialization.XmlIgnoreAttribute()> _ Public shipDateSpecified As Boolean <System.Xml.Serialization.XmlAttributeAttribute()> _ Public partNum As StringEnd ClassEnd Namespace
- 将以下描述订单的 XSD 架构保存为 Po.xsd 文件:
- 若要使用 Xsd.exe 工具创建可序列化的类,请按照下列步骤操作: 在创建可序列化的类之后,打开 Visual Studio .NET 以创建新的序列化项目。
- 若要使用 Xsd.exe 工具创建可序列化的类,请按照下列步骤操作: 新建 C# 或 Visual Basic .NET 控制台应用程序。
- 若要使用 Xsd.exe 工具创建可序列化的类,请按照下列步骤操作: 将 Xsd.exe 生成的类文件插到您的项目中。 为此,在解决方案资源管理器窗口中右键单击您的项目,单击添加,选择添加现有项,然后浏览以前生成的类文件。
- 若要使用 Xsd.exe 工具创建可序列化的类,请按照下列步骤操作: 确保该项目包含对上述步骤中创建的 System.Xml 和 XmlSerializationHowTo 名称空间的引用。 在 Xml 名称空间上使用 IMPORTS 语句,这样,以后就不需要在代码中的这些名称空间限定 XmlSerializer 声明了。IMPORTS 语句必须位于任何其他声明之前。 Visual Basic .NET 代码 C# 代码
Imports System.Xml.SerializationImports System.IOImports XmlSerializationHowTo
using System.Xml.Serialization;using System.IO;using XmlSerializationHowTo;
- 若要使用 Xsd.exe 工具创建可序列化的类,请按照下列步骤操作: 创建 XmlSerializer 类的一个实例,并传递要反序列化的对象的类型。 该示例使用以前定义的 PurchaseOrder 类型。 Visual Basic .NET 代码 C# 代码
Dim serializer As XmlSerializer = New XmlSerializer(GetType(PurchaseOrder))
XmlSerializer serializer = new XmlSerializer(typeof(PurchaseOrder));
- 若要使用 Xsd.exe 工具创建可序列化的类,请按照下列步骤操作: 若要读取该文件,请调用 Deserialize 方法,并传递到 Stream、TextReader 或 XmlReader 类中。 此时,就会返回一个订单。 在该示例中,使用以下代码: Visual Basic .NET 代码 C# 代码
Dim reader As TextReader = New StreamReader("po.xml")Dim po As PurchaseOrder = CType(serializer.Deserialize(reader), PurchaseOrder)reader.Close()
Po.xml 包含与订单关联的数据,这些订单遵循以前定义的 XSD 架构。 下面是该文件的内容:TextReader reader = new StreamReader("po.xml");PurchaseOrder po = (PurchaseOrder)serializer.Deserialize(reader);reader.Close();
<?xml version='1.0' encoding='utf-16'?><purchaseOrder orderDate="1999-10-20"> <shipTo country="US"> <name>Alice Smith</name> <street>123 Maple Street</street> <city>Mill Valley</city><state>CA</state> <zip>90952</zip> </shipTo> <billTo country="US"> <name>Robert Smith</name> <street>8 Oak Avenue</street> <city>Old Town</city><state>PA</state> <zip>95819</zip> </billTo> <comment>Hurry, my lawn is going wild!</comment> <items> <item partNum="872-AA"> <productName>Lawnmower</productName> <quantity>1</quantity> <price>148.95</price> <comment>Confirm this is electric</comment> </item> <item partNum="926-AA"> <productName>Baby Monitor</productName> <quantity>1</quantity> <price>39.98</price> <shipDate>1999-05-21</shipDate> </item> </items></purchaseOrder>
- 若要使用 Xsd.exe 工具创建可序列化的类,请按照下列步骤操作: 按照操作任何对象的一般方式操作 PurchaseOrder 对象(通过 PO 变量)。
- 若要使用 Xsd.exe 工具创建可序列化的类,请按照下列步骤操作: 将修改后的数据保存到一个新的 XML 文件中。 若要写入该文件,请调用 Serialize 方法,并传递到 Stream、TextReader 或 XmlReader 类以及订单的实例中: Visual Basic .NET 代码 C# 代码
Dim writer As TextWriter = New StreamWriter("po2.xml")serializer.Serialize(writer, po)writer.Close()
TextWriter writer = new StreamWriter("po2.xml");serializer.Serialize(writer, po);writer.Close();
- 若要使用 Xsd.exe 工具创建可序列化的类,请按照下列步骤操作: 保存并关闭项目。
- 如何将对象读写到 XML 中
- Visual Basic中如何利用DOM对象将数据输出到XML文档中
- Dom4j将xml文档信息封装到对象中
- SAX将xml文档信息封装到对象中
- 将xml文档内容封装到对象中
- 如何将DataSet对象存储到数据库中
- 如何将对象持久化到数据库中BLOB
- 将字符串读写到文件中
- freemarker将文件读写到HTML中
- XML:将信息写入到XML中
- Asp.net中如何将DataSet写入xml文件,及如何将xml读出来绑定到DataGrid(原创)
- Asp.net中如何将DataSet写入xml文件,及如何将xml读出来绑定到DataGrid
- Asp.net中如何将DataSet写入xml文件,及如何将xml读出来绑定到DataGrid
- XML解析之SAX解析:如何将XML信息存储到集合中
- 如何将xml文件转变成java对象
- Android 开发中 如何将xml文件封装成对象,存放到数组中?
- FxCAD 实验四 将FxCAD中的对象保存到XML文件中
- 将 Xml 文章通过对象的形式保存到 缓存中
- SQL2000/2005字符串拆分为列表通用函数
- C语言常见命名规范
- struct和typedef struct
- 从今天开始复习Hibernate
- 谁有java方面的中英文对照的技术介绍,毕设用,急用。。。
- 如何将对象读写到 XML 中
- SimpleDateFormat must be synchronized by yourself.
- 我的积分呢
- C++中的几个关键字:static,const,inline
- ocx控件引用
- SWT界面编程
- ABAP AT SELECTION-SCREEN写F4帮助
- Oracle dead lock deep research.
- Java调试相关