http://topic.csdn.net/u/20100819/16/f2fd30cf-8973-474b-8621-ce6f28bcd9a2.html
回复该帖子得到如此多收获,这种感觉真是太妙了!
我岂能不总结整理乎?!!
我们先看看XML文件究竟长啥样 :)
<?xml version="1.0" encoding="utf-8" ?> <shop> <province provinceName="安徽省"> <city cityName="合肥市"> <area areaName="庐阳区"> <LNshop shopName="A" address="A地址"/> <LNshop shopName="B" address="B地址"/> <LNshop shopName="C" address="C地址"/> </area> </city> <city cityName="淮北市"> <area areaName="相山区"> <LNshop shopName="D" address="D地址"/> </area> </city> </province> </shop>
方法一(应该是最为我们熟悉的方法):
XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(@"D:/XMLFile1.xml"); List<Hashtable> list = new List<Hashtable>(); XmlNodeList nodeList = xmlDoc.SelectSingleNode("shop").ChildNodes; //获取根节点的所有子节点 foreach (XmlNode provinceList in nodeList) //遍历所有province节点 { foreach (XmlNode cityList in provinceList) //遍历所有city节点 { foreach (XmlNode areaList in cityList) //遍历所有area节点 { foreach (XmlNode shopList in areaList) //遍历所有LNshop节点 { Hashtable ht = new Hashtable(); ht.Add("provinceName", provinceList.Attributes["provinceName"].Value); ht.Add("cityName", cityList.Attributes["cityName"].Value); ht.Add("areaName", areaList.Attributes["areaName"].Value); ht.Add("shopName", shopList.Attributes["shopName"].Value); ht.Add("address", shopList.Attributes["address"].Value); list.Add(ht); } } } } foreach (Hashtable data in list) { Console.WriteLine("{0}, {1}, {2}, {3}, {4}", data["provinceName"], data["cityName"], data["areaName"], data["shopName"], data["address"]); }
方法二:
XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(@"D:/XMLFile1.xml"); List<Hashtable> list = new List<Hashtable>(); XmlNodeList nodes = xmlDoc.SelectNodes("//LNshop"); //获取LNshop节点 foreach (XmlNode node in nodes) { Hashtable ht = new Hashtable(); ht.Add("provinceName", node.SelectSingleNode("ancestor::province").Attributes["provinceName"].Value); ht.Add("cityName", node.SelectSingleNode("ancestor::city").Attributes["cityName"].Value); ht.Add("areaName", node.SelectSingleNode("ancestor::area").Attributes["areaName"].Value); ht.Add("shopName", node.Attributes["shopName"].Value); ht.Add("address", node.Attributes["address"].Value); list.Add(ht); } foreach (Hashtable data in list) { Console.WriteLine("{0}, {1}, {2}, {3}, {4}", data["provinceName"], data["cityName"], data["areaName"], data["shopName"], data["address"]); }
下面隆重推出方法三(LinqToXML C#版)
// 加载指定的xml文件 XDocument xml = XDocument.Load(@"D:/XMLFile1.xml"); var list = from node in xml.Descendants("LNshop") //获取LNshop节点 select new { /* Ancestors 获取上级元素的集合 * 欲获取上级元素的属性无法直接用Attribute获取, * 只能通过Attributes(没错,就多了个s)获取,且通过Single()方法获取其属性值 */ provinceName = node.Ancestors("province").Attributes("provinceName").Single().Value, cityName = node.Ancestors("city").Attributes("cityName").Single().Value, areaName = node.Ancestors("area").Attributes("areaName").Single().Value, shopName = node.Attribute("shopName").Value, shopAddress = node.Attribute("address").Value }; foreach (var data in list) { Console.WriteLine("{0}, {1}, {2}, {3}, {4}", data.provinceName, data.cityName, data.areaName, data.shopName, data.shopAddress); }
我们不妨看看LinqToXML VB版
Dim xmlDoc As XDocument = XDocument.Load("D:/XMLFile1.xml") Dim List = From el In xmlDoc.<shop>.<province>.<city>.<area>.Elements _ Select el.Ancestors("province").@provinceName, _ el.Ancestors("city").@cityName, _ el.Ancestors("area").@areaName, _ el.@shopName, _ el.@address For Each s In List Console.WriteLine("{0}, {1}, {2}, {3}, {4}", s.provinceName, s.cityName, s.areaName, s.shopName, s.address) Next
VB版看起来更简洁,赞~/(≧▽≦)/~
谢谢xiaoniaodewen的帖子,也同时感谢fangxinggood与我们分享TA的好方法 :)