c# 创建XML文档,解析XML文档
1.静态函数
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml; namespace httpWebRequest { //CreateXMLList //<?xml version="1.0" encoding="utf-8"?> //<root> // <parent> // <son> // <name>lady gaga</name> // <age>50</age> // </son> // <son> // <name>swift</name> // <age>42</age> // </son> // </parent> //</root> //CreateXML //<?xml version="1.0" encoding="utf-8"?> //<root> // <parent> // <son> // <name>swift</name> // </son> // </parent> //</root> public class OperateXML { public OperateXML() { } /// <summary> /// 创建含有大量子节点的 /// </summary> public static void CreateXMLList(string parentName, string SonName, List<Dictionary<string, string>> XmlNodeInfoList) { XmlDocument myXml = new XmlDocument(); //创建声明 XmlDeclaration xd = myXml.CreateXmlDeclaration("1.0", "utf-8", null); myXml.AppendChild(xd); //创建根结点 XmlElement xmlRoot = myXml.CreateElement("root"); myXml.AppendChild(xmlRoot); //创建 parent XmlElement parent = myXml.CreateElement(parentName); xmlRoot.AppendChild(parent); foreach (Dictionary<string, string> xmlNodeInfo in XmlNodeInfoList) { //在parent下面创建son XmlElement son = myXml.CreateElement(SonName); parent.AppendChild(son); foreach (string key in xmlNodeInfo.Keys) { //在son下面创建name XmlElement name = myXml.CreateElement(key); name.InnerText = xmlNodeInfo[key]; son.AppendChild(name); } } //保存文档 myXml.Save("d:\\test.xml"); } public static void CreateXML() { XmlDocument myXml = new XmlDocument(); //创建声明 XmlDeclaration xd = myXml.CreateXmlDeclaration("1.0", "utf-8", null); myXml.AppendChild(xd); //创建根结点 XmlElement xmlRoot = myXml.CreateElement("root"); myXml.AppendChild(xmlRoot); //创建 parent XmlElement parent = myXml.CreateElement("parent"); xmlRoot.AppendChild(parent); //在parent下面创建son XmlElement son = myXml.CreateElement("son"); parent.AppendChild(son); //在son下面创建name XmlElement name = myXml.CreateElement("name"); name.InnerText = "swift"; son.AppendChild(name); //保存文档 myXml.Save("d:\\test.xml"); } public static List<string> ParseXML(ref string errStr, string path) { List<string> nameList = new List<string>(); XmlDocument myXml = new XmlDocument(); try { myXml.Load("d:\\test.xml"); } catch (Exception e) { errStr = string.Format("load xml is failed"); return null; } //string path = "root"; //XmlNode xmlRoot = myXml.SelectSingleNode(path); //if (xmlRoot == null) //{ // errStr = string.Format("do not have this path:{0}", path); // return null; //} //path = "parent/son"; XmlNodeList xmlNodeList = myXml.SelectNodes(path); if (xmlNodeList == null || xmlNodeList.Count <= 0) { errStr = string.Format("do not have this path:{0}", path); return null; } foreach (XmlNode xmlNode in xmlNodeList) { string name = xmlNode.InnerText; nameList.Add(name); } return nameList; } } }
2.使用
private void button1_Click(object sender, EventArgs e) { Dictionary<string, string> dt = new Dictionary<string, string>() { { "name", "lady gaga" }, { "age", "50" } }; Dictionary<string, string> dt1 = new Dictionary<string, string>() { { "name", "swift" }, { "age", "42" } }; List<Dictionary<string, string>> XmlNodeInfoList = new List<Dictionary<string, string>>(); XmlNodeInfoList.Add(dt); XmlNodeInfoList.Add(dt1); OperateXML.CreateXMLList("parent", "son", XmlNodeInfoList); } private void button2_Click(object sender, EventArgs e) { string errStr = string.Empty; OperateXML.ParseXML(ref errStr,"root/parent/son"); } private void button3_Click(object sender, EventArgs e) { OperateXML.CreateXML(); }