这个适合刚学asp.net的同学,大神直接略过好了,asp.net经常会有很多用到XML的地方,比如全国省市的联动,以及一些菜单读取等等都有xml的影子,直接贴代码,以便我以后用到的时候忘了,注释我写得很清楚了,不懂的可以留言。
class="brush:csharp;gutter:true;">/// <summary> /// 读取城市列表 /// </summary> public void CityList() { StringBuilder sb = new StringBuilder(); XmlDocument xmldoc = new XmlDocument(); xmldoc.Load(Server.MapPath("/XmlData/City.xml"));//加载xml XmlNodeList xmlnlist = xmldoc.SelectNodes("/Cities/City");//读取xml的节点列表 for (int i = 0; i < xmlnlist.Count; i++) { sb.Append("<a href=\"index.aspx?cityid=" + xmlnlist[i].Attributes["ID"] .Value+ "\">" + xmlnlist[i].Attributes["CityName"] .Value+ "</a>");//循环读取里面的值 } citys = sb.ToString(); } ////------附xml格式------///////////////// <?xml version="1.0" encoding="utf-8"?> <Cities> <City ID="218" CityName="南宁" PID="20" ZipCode="530000">南宁</City> <City ID="219" CityName="柳州" PID="20" ZipCode="545000">柳州</City> <City ID="228" CityName="贺州" PID="20" ZipCode="542800">贺州</City> </Cities>