using System.IO; using System.Xml.Serialization; private void TestXmlSerialize() { //原创来自 http://www.luofenming.com/show.aspx?id=ART2018112100001转载请保留此地址 string ConfigPath = AppDomain.CurrentDomain.BaseDirectory + "JH.xml"; string xmlData = string.Empty; using (StreamReader sr = new StreamReader(ConfigPath)) { xmlData = sr.ReadToEnd();//读取xml文件 } Config config; XmlSerializer xs = new XmlSerializer(typeof(Config));//序列化成实体Config using (Stream stream = new MemoryStream(Encoding.UTF8.GetBytes(xmlData))) { config = (Config)xs.Deserialize(stream);//反序列化成实体Config } } #region 实体 [Serializable] [XmlRoot(ElementName = "Config")] public class Config { [XmlArray("Company")] public List<CompanyItem> Company { get; set; } [XmlArray("Property")] public List<PropertyItem> Property { get; set; } [XmlArray("Product")] public List<ProductItem> Product { get; set; } [XmlArray("Unit")] public List<UnitItem> Unit { get; set; } } [Serializable] [XmlType(TypeName = "companyItem")] public class CompanyItem { [XmlAttribute(AttributeName = "name")] public string name { get; set; } [XmlText] public string innerText { get; set; } } [Serializable] [XmlType(TypeName = "propertyItem")] public class PropertyItem { [XmlAttribute(AttributeName = "name")] public string name { get; set; } [XmlText] public string innerText { get; set; } } [Serializable] [XmlType(TypeName = "productItem")] public class ProductItem { [XmlAttribute(AttributeName = "name")] public string name { get; set; } [XmlText] public string innerText { get; set; } } [Serializable] [XmlType(TypeName = "unitItem")] public class UnitItem { [XmlAttribute(AttributeName = "name")] public string name { get; set; } [XmlText] public string innerText { get; set; } } #endregion
以下是JH.xml文件里面内容
<Config> <Company> <companyItem name="国网公司总部">01</companyItem> <companyItem name="国网华北分部">02</companyItem> <companyItem name="国网华东分部">03</companyItem> <companyItem name="国网华中分部">04</companyItem> <companyItem name="国网东北分部">05</companyItem> <companyItem name="国网西北分部">06</companyItem> <companyItem name="国网北京电力">11</companyItem> <companyItem name="国网天津电力">12</companyItem> <companyItem name="国网河北电力">13</companyItem> <companyItem name="国网山西电力">14</companyItem> <companyItem name="国网蒙东电力">15</companyItem> <companyItem name="国网冀北电力">16</companyItem> <companyItem name="国网辽宁电力">21</companyItem> <companyItem name="国网吉林电力">22</companyItem> <companyItem name="国网黑龙江电力">23</companyItem> <companyItem name="国网上海电力">31</companyItem> <companyItem name="国网江苏电力">32</companyItem> <companyItem name="国网浙江电力">33</companyItem> <companyItem name="国网安徽电力">34</companyItem> <companyItem name="国网福建电力">35</companyItem> <companyItem name="国网江西电力">36</companyItem> <companyItem name="国网山东电力">37</companyItem> <companyItem name="国网河南电力">41</companyItem> <companyItem name="国网湖北电力">42</companyItem> <companyItem name="国网湖南电力">43</companyItem> <companyItem name="国网重庆电力">50</companyItem> <companyItem name="国网西藏电力">54</companyItem> <companyItem name="国网陕西电力">61</companyItem> <companyItem name="国网甘肃电力">62</companyItem> <companyItem name="国网青海电力">63</companyItem> <companyItem name="国网宁夏电力">64</companyItem> <companyItem name="国网新疆电力">65</companyItem> <companyItem name="国网计量中心">70</companyItem> <companyItem name="中国电科院">71</companyItem> <companyItem name="南瑞集团">72</companyItem> <companyItem name="国网运行公司">73</companyItem> <companyItem name="国网直流公司">74</companyItem> <companyItem name="国网交流公司">75</companyItem> </Company> <Property> <propertyItem name="电能表">01</propertyItem> <propertyItem name="互感器">02</propertyItem> <propertyItem name="计量箱(屏、柜)">05</propertyItem> <propertyItem name="电能信息采集终端">09</propertyItem> <propertyItem name="计量标准">10</propertyItem> <propertyItem name="测试装置">13</propertyItem> <propertyItem name="其他仪器仪表">14</propertyItem> <propertyItem name="周转箱(托盘)">19</propertyItem> <propertyItem name="现场手持终端">20</propertyItem> <propertyItem name="反窃电装置">51</propertyItem> <propertyItem name="流水线设备">52</propertyItem> <propertyItem name="仓储设备">53</propertyItem> <propertyItem name="通信模块">54</propertyItem> </Property> <Product> <productItem name="单相计量故障识别模块">02</productItem> <productItem name="三相计量故障识别模块">03</productItem> <productItem name="采集故障识别模块">04</productItem> <productItem name="超高频RFID模块">05</productItem> </Product> <Unit> <unitItem name="客户">0</unitItem> <unitItem name="公司总部">1</unitItem> <unitItem name="公司分部">2</unitItem> <unitItem name="省级电力公司">3</unitItem> <unitItem name="直属单位">4</unitItem> </Unit> </Config>