自己用。记录一下!
用于配置文件的解析,可以用来设置配置。不用修改程序里参数。
用微软的XML 解析器来解析的。
1. Xml文件
文件名称:TestConfig.xml
<?xml version="1.0"?> <Config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <PowerLine> <Version>1.01</Version> <FieldNum>10</FieldNum> <Fields> <Field name="STARTID">起始ID</Field> <Field name="FNODE">起点</Field> <Field name="TNODE">终点</Field> <Field name="POWERID">线路ID</Field> </Fields> </PowerLine> </Config>
2. 对类的结构定义
文件名称:ParaseConfig.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml; using System.Xml.Serialization; // namespace PowerCongfig { // 名称和对应值 public class FieldNode { [XmlAttribute("name")] public string name { get; set; } [XmlText] public string Value{ get; set; } } // 名称和对应值 public class FieldsNode { [XmlElement("Field")] public List<FieldNode> oFieldNode { get; set; } } // public class PowerLine { [XmlElement("Version")] // 版本 public double dVersion; [XmlElement("FieldNum")] // 属性字段个数 public int CountNode { get; set; } [XmlElement("Fields")] public FieldsNode oFieldsNode { get; set; } } [XmlRoot("Config")] public class TestRoot { [XmlElement("PowerLine")] public PowerLine oPowerLine { get; set; } } }
3. 反序列化解析Xml
文件名称:
Program.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; namespace PowerCongfig { class Program { static void Main(string[] args) { // BuildXMlCofig(); ParaseXMLConfig(); } // 用来生成Xml文件 private static void BuildXMlCofig() { TestRoot oTestRoot = new TestRoot(); oTestRoot.oPowerLine = new PowerLine(); oTestRoot.oPowerLine.dVersion = 1.01; oTestRoot.oPowerLine.CountNode = 10; // FieldNode oFieldNode0 = new FieldNode(); oFieldNode0.name="STARTID"; oFieldNode0.Value="起始ID"; FieldNode oFieldNode1 = new FieldNode(); oFieldNode1.name="FNODE"; oFieldNode1.Value="起点"; FieldNode oFieldNode2 = new FieldNode(); oFieldNode2.name="TNODE"; oFieldNode2.Value="终点"; FieldNode oFieldNode3 = new FieldNode(); oFieldNode3.name="POWERID"; oFieldNode3.Value="线路ID"; // oTestRoot.oPowerLine.oFieldsNode = new FieldsNode(); oTestRoot.oPowerLine.oFieldsNode.oFieldNode = new List<FieldNode>(); oTestRoot.oPowerLine.oFieldsNode.oFieldNode.Add(oFieldNode0); oTestRoot.oPowerLine.oFieldsNode.oFieldNode.Add(oFieldNode1); oTestRoot.oPowerLine.oFieldsNode.oFieldNode.Add(oFieldNode2); oTestRoot.oPowerLine.oFieldsNode.oFieldNode.Add(oFieldNode3); // 序列化 // 保存文件名称 string oFileName = "C:\my_Test.xml"; System.Xml.Serialization.XmlSerializer serializer = null; System.IO.StreamWriter writer = null; try { serializer = new System.Xml.Serialization.XmlSerializer(oTestRoot.GetType()); FileStream fs = new FileStream(oFileName, FileMode.Create); serializer.Serialize(fs, oTestRoot); } catch (System.Exception ex) { string mes = ex.Message; } finally { if (writer != null) { writer.Close(); } } } // 解析Xml到类对象中。 private static void ParaseXMLConfig() { TestRoot oTestRoot = new TestRoot(); oTestRoot.oPowerLine = new PowerLine(); oTestRoot.oPowerLine.oFieldsNode = new FieldsNode(); oTestRoot.oPowerLine.oFieldsNode.oFieldNode = new List<FieldNode>(); string oFileName = @"D:zhangpengjuCodeTest estcplusTestConfig.xml"; System.Xml.Serialization.XmlSerializer serializer = null; System.IO.StreamWriter writer = null; try { serializer = new System.Xml.Serialization.XmlSerializer(oTestRoot.GetType()); FileStream fs = new FileStream(oFileName, FileMode.Open); oTestRoot = (TestRoot)serializer.Deserialize(fs); } catch (System.Exception ex) { string mes = ex.Message; } finally { if (writer != null) { writer.Close(); } } // Console.WriteLine("Version is {0}",oTestRoot.oPowerLine.dVersion); Console.ReadLine(); } } }
就这样。自己保存!
免分源码地址:http://download.csdn.net/detail/cartzhang/6270007
求进步!