解析XML

Xml解析方法:
1
public static Dictionary<int, List<string>> ReadingXml(string xmlTxt,string FatherName) 2 { 4 XmlDocument xmlDoc = new XmlDocument(); 5 //加载xml文档 6 xmlDoc.LoadXml(xmlTxt); 7 //查找FatherName 【FatherName:xml文档最外层父节点】 8 XmlNode root = xmlDoc.SelectSingleNode(FatherName); 9 10 //获取到所有FatherName的子节点 11 XmlNodeList nodeList = xmlDoc.SelectSingleNode(FatherName).ChildNodes;//item 节点 12 13 List<string> list = null; 14 Dictionary<int, List<string>> dic = new Dictionary<int, List<string>>(); 15 int key = 0; 16 //遍历所有子节点 17 foreach (XmlNode xn in nodeList) 18 { 19 XmlElement xe = (XmlElement)xn; //item 20 XmlNodeList subList = xe.ChildNodes;//item的子节点 21 22 list = new List<string>(); 23 foreach (XmlNode xmlNode in subList) 24 { 25 //所需要的值 26 list.Add(xmlNode.InnerText); 27 } 28 dic.Add(key, list); 29 key++; 30 } 31 return dic; 32 }

 xml 文件比较灵活,节点里面的内容都是根据需求设定,解析xml需要根据xml文档的实际情况和需求情况来确定方法的编写,上述方法适用于简单的xml解析.

posted @ 2015-08-10 09:58  过客丶  阅读(448)  评论(0编辑  收藏  举报