xml反串行化

  1 using  System;
  2 using  System.Collections.Generic;
  3 using  System.Text;
  4
  5 using  System.Xml.Serialization;
  6
  7 namespace  DBSchema2Doc
  8 {
  9    [Serializable]
 10    [XmlRoot("Root")]
 11    public class Settings
 12    {
 13        public Settings()
 14        {
 15
 16        }

 17        private DefaultSettings m_DefaultSettings;
 18        private SavedSettings m_SavedSettings;
 19        [XmlElement("DefaultSettings")]
 20        public DefaultSettings DefaultSettings
 21        {
 22            get
 23            {
 24                return this.m_DefaultSettings;
 25            }

 26            set
 27            {
 28                this.m_DefaultSettings = value;
 29            }

 30        }

 31        [XmlElement("SavedSettings")]
 32        public SavedSettings SavedSettings
 33        {
 34            get
 35            {
 36                return this.m_SavedSettings;
 37            }

 38            set
 39            {
 40                this.m_SavedSettings = value;
 41            }

 42        }

 43        public override string ToString()
 44        {
 45            return this.m_DefaultSettings.ToString();
 46        }

 47    }

 48    [Serializable]
 49    public class DefaultSettings
 50    {
 51        public DefaultSettings()
 52        {
 53        }

 54        private DefaultSetting[] m_DefaultSetting;
 55        [XmlElement("DefaultSetting")]
 56        public DefaultSetting[] DefaultSetting
 57        {
 58            get
 59            {
 60                return this.m_DefaultSetting;
 61            }

 62            set
 63            {
 64                this.m_DefaultSetting = value;
 65            }

 66        }

 67        public override string ToString()
 68        {
 69            string temp = "";
 70            foreach (DefaultSetting ds in this.m_DefaultSetting)
 71            {
 72                temp += "\n" + ds.ToString();
 73            }

 74            return temp;
 75        }

 76
 77    }

 78    [Serializable]
 79    public class DefaultSetting
 80    {
 81        public DefaultSetting()
 82        {
 83
 84        }

 85        private string m_name;
 86        private string m_value;
 87        [XmlAttribute("name")]
 88        public string name
 89        {
 90            get
 91            {
 92                return this.m_name;
 93            }

 94            set
 95            {
 96                this.m_name = value;
 97            }

 98        }

 99        [XmlAttribute("value")]
100        public string value
101        {
102            get
103            {
104                return this.m_value;
105            }

106            set
107            {
108                this.m_value = value;
109            }

110        }

111        public override string ToString()
112        {
113            return string.Format("\n name={0} value={1}"this.m_name, this.m_value);
114        }

115
116    }

117    [Serializable]
118    public class SavedSettings
119    {
120        public SavedSettings()
121        {
122
123        }

124        private SavedSetting[] m_SavedSetting;
125        [XmlElement("SavedSetting")]
126        public SavedSetting[] SavedSetting
127        {
128            get
129            {
130                return this.m_SavedSetting;
131            }

132            set
133            {
134                this.m_SavedSetting = value;
135            }

136        }

137    }

138    [Serializable]
139    public class SavedSetting
140    {
141        public SavedSetting()
142        {
143
144        }

145        private string m_connstr;
146        private string m_DBType;
147        private string m_name;
148        [XmlAttribute("connstr")]
149        public string connstr
150        {
151            get
152            {
153                return this.m_connstr;
154            }

155            set
156            {
157                this.m_connstr = value;
158            }

159        }

160        [XmlAttribute("DBType")]
161        public string DBType
162        {
163            get
164            {
165                return this.m_DBType;
166            }

167            set
168            {
169                this.m_DBType = value;
170            }

171        }

172        [XmlAttribute("name")]
173        public string name
174        {
175            get
176            {
177                return this.m_name;
178            }

179            set
180            {
181                this.m_name = value;
182            }

183        }

184
185        public override string ToString()
186        {
187            return string.Format("\nconnstr={0} DBType={1} name={2}"this.m_connstr, this.m_DBType, this.m_name);
188        }

189    }

190
191
192}

193

 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 >

 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 >

 

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);

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 {
  9    [Serializable]
 10    [XmlRoot("Root")]
 11    public class NewSettings
 12    {
 13        public NewSettings()
 14        {
 15
 16        }

 17        private DefaultSetting[] m_DefaultSetting;
 18        private SavedSetting[] m_SavedSetting;
 19        [XmlArray(ElementName="DefaultSettings")]
 20        [XmlArrayItem(ElementName="DefaultSetting")]
 21        public DefaultSetting[] DefaultSetting
 22        {
 23            get
 24            {
 25                return this.m_DefaultSetting;
 26            }

 27            set
 28            {
 29                this.m_DefaultSetting = value;
 30            }

 31        }

 32        [XmlArray(ElementName="SavedSettings")]
 33        [XmlArrayItem(ElementName="SavedSetting")]
 34        public SavedSetting[] SavedSetting
 35        {
 36            get
 37            {
 38                return this.m_SavedSetting;
 39            }

 40            set
 41            {
 42                this.m_SavedSetting = value;
 43            }

 44        }

 45        public override string ToString()
 46        {
 47            return base.ToString();
 48        }

 49    }

 50    [Serializable]
 51    public class DefaultSetting
 52    {
 53        public DefaultSetting()
 54        {
 55
 56        }

 57        private string m_name;
 58        private string m_value;
 59        [XmlAttribute("name")]
 60        public string name
 61        {
 62            get
 63            {
 64                return this.m_name;
 65            }

 66            set
 67            {
 68                this.m_name = value;
 69            }

 70        }

 71        [XmlAttribute("value")]
 72        public string value
 73        {
 74            get
 75            {
 76                return this.m_value;
 77            }

 78            set
 79            {
 80                this.m_value = value;
 81            }

 82        }

 83        public override string ToString()
 84        {
 85            return string.Format("\n name={0} value={1}"this.m_name, this.m_value);
 86        }

 87
 88    }

 89    [Serializable]
 90    public class SavedSetting
 91    {
 92        public SavedSetting()
 93        {
 94
 95        }

 96        private string m_connstr;
 97        private string m_DBType;
 98        private string m_name;
 99        [XmlAttribute("connstr")]
100        public string connstr
101        {
102            get
103            {
104                return this.m_connstr;
105            }

106            set
107            {
108                this.m_connstr = value;
109            }

110        }

111        [XmlAttribute("DBType")]
112        public string DBType
113        {
114            get
115            {
116                return this.m_DBType;
117            }

118            set
119            {
120                this.m_DBType = value;
121            }

122        }

123        [XmlAttribute("name")]
124        public string name
125        {
126            get
127            {
128                return this.m_name;
129            }

130            set
131            {
132                this.m_name = value;
133            }

134        }

135
136        public override string ToString()
137        {
138            return string.Format("\nconnstr={0} DBType={1} name={2}"this.m_connstr, this.m_DBType, this.m_name);
139        }

140    }

141
142
143}

144

你可能感兴趣的:(xml)