C#创建Xml文档
半野
1
`using System; using System.Data; using System.Xml; namespace XmlTest { class Program { static void Main(string[] args) { //xml数据来源的构建 DataTable dt=new DataTable(); dt.Columns.Add("x", typeof (string)); dt.Columns.Add("y", typeof(string)); dt.Rows.Add("33.5", "139.2"); dt.Rows.Add("34.1", "128.7"); dt.Rows.Add("43.7", "145.4"); XmlDocument xmlDoc = new XmlDocument(); //创建类型声明节点 XmlNode stateNode = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", ""); xmlDoc.AppendChild(stateNode); //创建根节点 XmlNode root = xmlDoc.CreateElement("root"); xmlDoc.AppendChild(root); foreach (DataRow dr in dt.Rows) { XmlNode node = xmlDoc.CreateNode(XmlNodeType.Element, "XY", null); CreateNode(xmlDoc, node, "X", dr.ItemArray[0].ToString()); CreateNode(xmlDoc, node, "Y", dr.ItemArray[1].ToString()); root.AppendChild(node); } xmlDoc.Save("../../../xmlTest.xml"); Console.WriteLine("Ok"); Console.ReadLine(); } public static void CreateNode(XmlDocument xmlDoc, XmlNode parentNode, string name, string value) { XmlNode node = xmlDoc.CreateNode(XmlNodeType.Element, name, null); node.InnerText = value; parentNode.AppendChild(node); } } }`
由 Hexo 驱动 & 主题 Keep
总字数 105.7k 访客数 访问量