1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4
5
using System.Xml.Serialization;
6
7
namespace DBSchema2Doc
8
2
3
4
5
6
7
8
1
<?xml version="1.0" encoding="utf-8" ?>
2
<Root>
3
<DefaultSettings>
4
<DefaultSetting name="Microsoft SQLServer" value="SQL"/>
5
<DefaultSetting name="Oracle" value="ORACLE"/>
6
</DefaultSettings>
7
<SavedSettings>
8
<SavedSetting connstr="" DBType="DB2" name="test"/>
9
<SavedSetting connstr="" DBType="MySQL" name="test2"/>
10
</SavedSettings>
11
</Root>
2
3
4
5
6
7
8
9
10
11
1
<?xml version="1.0" encoding="utf-8"?>
2
<xs:schema id="Root" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
3
<xs:element name="Root" msdata:IsDataSet="true" msdata:Locale="en-US">
4
<xs:complexType>
5
<xs:choice minOccurs="0" maxOccurs="unbounded">
6
<xs:element name="DefaultSettings">
7
<xs:complexType>
8
<xs:sequence>
9
<xs:element name="DefaultSetting" minOccurs="0" maxOccurs="unbounded">
10
<xs:complexType>
11
<xs:attribute name="name" type="xs:string" />
12
<xs:attribute name="value" type="xs:string" />
13
</xs:complexType>
14
</xs:element>
15
</xs:sequence>
16
</xs:complexType>
17
</xs:element>
18
<xs:element name="SavedSettings">
19
<xs:complexType>
20
<xs:sequence>
21
<xs:element name="SavedSetting" minOccurs="0" maxOccurs="unbounded">
22
<xs:complexType>
23
<xs:attribute name="connstr" type="xs:string" />
24
<xs:attribute name="DBType" type="xs:string" />
25
<xs:attribute name="name" type="xs:string" />
26
</xs:complexType>
27
</xs:element>
28
</xs:sequence>
29
</xs:complexType>
30
</xs:element>
31
</xs:choice>
32
</xs:complexType>
33
</xs:element>
34
</xs:schema>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
xsd文件可以用来校验欲载入文件的格式,
利用XmlSerializer的Deserialize方法将xml文件还原为Settings格式的对象状态
1
XmlSerializer xs=new XmlSerializer(typeof(Settings));
2
FileStream fs = new FileStream("../../XMLFile1.xml",FileMode.Open);
3
Settings settings = (Settings)xs.Deserialize(fs);
2
3
Settings类还可以改进,减少到3个类,利用[XmlArrayItem(ElementName="")]和[XmlArray(ElementName="")]标记如下:
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4
5
using System.Xml.Serialization;
6
7
namespace DBSchema2Doc
8
2
3
4
5
6
7
8