根据xsd序列化与反序列化xml
1,建立xsd
网页题目:根据xsd序列化与反序列化xml
网页网址:http://azwzsj.com/article/pdogdc.html
step1:建立“类型”Version、Updatetime,Files,File
step2:建立Files与File的多对一关系,添加Files中的file引用(是File类型的),修改file的属性maxOccurs为unbounded,minOccurs为1
如图:
step3:建立顶级元素“类型”Update,添加version,updatetime,files的引用,如图
step4:添加顶级元素update(类型为Update),如图
xsd代码如下:
xml version="1.0" encoding="utf-8"?>
<xs:schema id="UpdateFile" targetNamespace="http://tempuri.org/UpdateFile.xsd" elementFormDefault="qualified" xmlns="http://tempuri.org/UpdateFile.xsd" xmlns:mstns="http://tempuri.org/UpdateFile.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="Update">
<xs:sequence>
<xs:element name="version" type="Version" />
<xs:element name="updatetime" type="Updatetime" />
<xs:element name="files" type="Files" />
xs:sequence>
xs:complexType>
<xs:complexType name="Version">
<xs:sequence>
<xs:element name="value" type="xs:string" />
<xs:element name="type" type="xs:string" />
xs:sequence>
xs:complexType>
<xs:complexType name="Updatetime">
<xs:sequence>
<xs:element name="value" type="xs:dateTime" />
xs:sequence>
xs:complexType>
<xs:complexType name="Files">
<xs:sequence>
<xs:element name="file" type="File" maxOccurs="unbounded" minOccurs="1" />
xs:sequence>
xs:complexType>
<xs:complexType name="File">
<xs:sequence>
<xs:element name="url" type="xs:string" />
xs:sequence>
xs:complexType>
<xs:element name="update" type="Update">
xs:element>
xs:schema>
<xs:schema id="UpdateFile" targetNamespace="http://tempuri.org/UpdateFile.xsd" elementFormDefault="qualified" xmlns="http://tempuri.org/UpdateFile.xsd" xmlns:mstns="http://tempuri.org/UpdateFile.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="Update">
<xs:sequence>
<xs:element name="version" type="Version" />
<xs:element name="updatetime" type="Updatetime" />
<xs:element name="files" type="Files" />
xs:sequence>
xs:complexType>
<xs:complexType name="Version">
<xs:sequence>
<xs:element name="value" type="xs:string" />
<xs:element name="type" type="xs:string" />
xs:sequence>
xs:complexType>
<xs:complexType name="Updatetime">
<xs:sequence>
<xs:element name="value" type="xs:dateTime" />
xs:sequence>
xs:complexType>
<xs:complexType name="Files">
<xs:sequence>
<xs:element name="file" type="File" maxOccurs="unbounded" minOccurs="1" />
xs:sequence>
xs:complexType>
<xs:complexType name="File">
<xs:sequence>
<xs:element name="url" type="xs:string" />
xs:sequence>
xs:complexType>
<xs:element name="update" type="Update">
xs:element>
xs:schema>
2,生成实体类
用vs命令行在项目文件夹下输入以下命令
xsd.exe 要生成实体类的.xsd /c /namespace:要生成的实体类的命名空间
3,两个静态工具类
class Common
{
///
/// 将XML文件写入指定的对象
///
/// "xmlFile">xml绝对路径
/// "type">序列的类型,要与XML对应的类
///将对象返回,当文件操作失败则返回Null值
public static object DeserializeXmlToObject(string xmlFile, Type type)
{
XmlSerializer mySerializer = new XmlSerializer(type);
using (FileStream stream = new FileStream(xmlFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
return mySerializer.Deserialize(stream);
}
}
///
/// 将对象写入到XML中
///
/// "obj">数据源对象
/// "xmlFile">目标路径
/// "type">转换类型
public static void SerializeObjectToXml(object obj, String xmlFile, Type type)
{
XmlSerializer mySerializer = new XmlSerializer(type);
using (FileStream stream = new FileStream(xmlFile, FileMode.Create, FileAccess.Write, FileShare.Read))
{
mySerializer.Serialize(stream, obj);
}
}
}
{
///
/// 将XML文件写入指定的对象
///
/// "xmlFile">xml绝对路径
/// "type">序列的类型,要与XML对应的类
///
public static object DeserializeXmlToObject(string xmlFile, Type type)
{
XmlSerializer mySerializer = new XmlSerializer(type);
using (FileStream stream = new FileStream(xmlFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
return mySerializer.Deserialize(stream);
}
}
///
/// 将对象写入到XML中
///
/// "obj">数据源对象
/// "xmlFile">目标路径
/// "type">转换类型
public static void SerializeObjectToXml(object obj, String xmlFile, Type type)
{
XmlSerializer mySerializer = new XmlSerializer(type);
using (FileStream stream = new FileStream(xmlFile, FileMode.Create, FileAccess.Write, FileShare.Read))
{
mySerializer.Serialize(stream, obj);
}
}
}
4,序列化与反序列化
//反序列化
Update a = new Update();
a.version = new Version();
a.version.type = "0";
a.version.value = "1.0.0.0";
a.updatetime = new Updatetime();
a.updatetime.value = new System.DateTime();
a.files = new File[1];
a.files[0] = new File();
a.files[0].url = "http://test.exe";
Common.SerializeObjectToXml(a, "目标.xml", typeof(Update));
//系列化
Update u = (Update)Common.DeserializeXmlToObject("目标.xml", typeof(Update));
Update a = new Update();
a.version = new Version();
a.version.type = "0";
a.version.value = "1.0.0.0";
a.updatetime = new Updatetime();
a.updatetime.value = new System.DateTime();
a.files = new File[1];
a.files[0] = new File();
a.files[0].url = "http://test.exe";
Common.SerializeObjectToXml(a, "目标.xml", typeof(Update));
//系列化
Update u = (Update)Common.DeserializeXmlToObject("目标.xml", typeof(Update));
5,gameover
网页题目:根据xsd序列化与反序列化xml
网页网址:http://azwzsj.com/article/pdogdc.html