1xml反串行化using System;
  2xml反串行化using System.Collections.Generic;
  3xml反串行化using System.Text;
  4xml反串行化
  5xml反串行化using System.Xml.Serialization;
  6xml反串行化
  7xml反串行化namespace DBSchema2Doc
  8

 1xml反串行化<?xml version="1.0" encoding="utf-8" ?>
 2xml反串行化<Root>
 3xml反串行化    <DefaultSettings>
 4xml反串行化        <DefaultSetting name="Microsoft SQLServer" value="SQL"/>
 5xml反串行化        <DefaultSetting name="Oracle" value="ORACLE"/>
 6xml反串行化    </DefaultSettings>
 7xml反串行化    <SavedSettings>
 8xml反串行化        <SavedSetting connstr="" DBType="DB2" name="test"/>
 9xml反串行化        <SavedSetting connstr="" DBType="MySQL" name="test2"/>
10xml反串行化       </SavedSettings>
11xml反串行化</Root>

 1xml反串行化<?xml version="1.0" encoding="utf-8"?>
 2xml反串行化<xs:schema id="Root" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
 3xml反串行化  <xs:element name="Root" msdata:IsDataSet="true" msdata:Locale="en-US">
 4xml反串行化    <xs:complexType>
 5xml反串行化      <xs:choice minOccurs="0" maxOccurs="unbounded">
 6xml反串行化        <xs:element name="DefaultSettings">
 7xml反串行化          <xs:complexType>
 8xml反串行化            <xs:sequence>
 9xml反串行化              <xs:element name="DefaultSetting" minOccurs="0" maxOccurs="unbounded">
10xml反串行化                <xs:complexType>
11xml反串行化                  <xs:attribute name="name" type="xs:string" />
12xml反串行化                  <xs:attribute name="value" type="xs:string" />
13xml反串行化                </xs:complexType>
14xml反串行化              </xs:element>
15xml反串行化            </xs:sequence>
16xml反串行化          </xs:complexType>
17xml反串行化        </xs:element>
18xml反串行化        <xs:element name="SavedSettings">
19xml反串行化          <xs:complexType>
20xml反串行化            <xs:sequence>
21xml反串行化              <xs:element name="SavedSetting" minOccurs="0" maxOccurs="unbounded">
22xml反串行化                <xs:complexType>
23xml反串行化                  <xs:attribute name="connstr" type="xs:string" />
24xml反串行化                  <xs:attribute name="DBType" type="xs:string" />
25xml反串行化                  <xs:attribute name="name" type="xs:string" />
26xml反串行化                </xs:complexType>
27xml反串行化              </xs:element>
28xml反串行化            </xs:sequence>
29xml反串行化          </xs:complexType>
30xml反串行化        </xs:element>
31xml反串行化      </xs:choice>
32xml反串行化    </xs:complexType>
33xml反串行化  </xs:element>
34xml反串行化</xs:schema>

 

xsd文件可以用来校验欲载入文件的格式,
利用XmlSerializer的Deserialize方法将xml文件还原为Settings格式的对象状态

1xml反串行化XmlSerializer xs=new XmlSerializer(typeof(Settings));
2xml反串行化                FileStream fs = new FileStream("../../XMLFile1.xml",FileMode.Open);
3xml反串行化                Settings settings = (Settings)xs.Deserialize(fs);

Settings类还可以改进,减少到3个类,利用[XmlArrayItem(ElementName="")]和[XmlArray(ElementName="")]标记如下:

  1xml反串行化using System;
  2xml反串行化using System.Collections.Generic;
  3xml反串行化using System.Text;
  4xml反串行化
  5xml反串行化using System.Xml.Serialization;
  6xml反串行化
  7xml反串行化namespace DBSchema2Doc
  8

相关文章: