C#中可以用XmlDocument类操作Xml文件
例如要读取如下Xml文件
1 <root> 2 person name="WangYao"3 age>25</4 person5 ="Jobs"6 >567 8 >
程式如下
1 XmlDocument doc = new XmlDocument();
2 doc.Load("config.xml"); //加载Xml文件
3 XmlElement rootElem = doc.DocumentElement; 获取根节点 4 XmlNodeList personNodes = rootElem.GetElementsByTagName(person"); 获取person子节点集合 5 foreach (XmlNode node in personNodes)
6 {
7 string strName = ((XmlElement)node).GetAttribute(name"); 获取name属性值 8 Console.WriteLine(strName);
9 XmlNodeList subAgeNodes = ((XmlElement)node).GetElementsByTagName(age"); 获取age子XmlElement集合 10 if (subAgeNodes.Count == 1)
11 {
12 string strAge = subAgeNodes[0].InnerText;
13 Console.WriteLine(strAge);
14 }
15 }
其中XmlElement继承自XmlNode
XmlElement有GetAttribute()&GetElementsByTagName()等方法而XmlNode没有
不管使用XmlNode的ChildNodes属性还是XmlElement的GetElementsByTagName()方法获取的都是XmlNodeList
那这里就存在获取的XmlNodeList中的XmlNode到底是什么类型的问题
可以根据XmlNode的NodeType属性判断
如若等于XmlNodeType.Element就可以强转为XmlElement从而使用XmlElement的方法
转载:http://www.cnblogs.com/Hisin/archive/2012/02/27/2370646.html
- usingSystem;@H_502_188@
- usingUnityEngine;@H_502_188@
- usingSystem.IO;@H_502_188@
- usingSystem.Xml;@H_502_188@
- usingSystem.Linq;@H_502_188@
- usingSystem.Text;@H_502_188@
- usingSystem.Collections.Generic;@H_502_188@
- @H_502_188@
- @H_502_188@
- namespaceAddress@H_502_188@
- {@H_502_188@
- ///<summary>@H_502_188@
- ///地址数据@H_502_188@
- ///</summary>@H_502_188@
- publicclassAddressData@H_502_188@
- {@H_502_188@
- ///当前城市ID@H_502_188@
- staticstring_NowProvinceId;@H_502_188@
- ///所有省名字@H_502_188@
- staticList<string>allProvinceName=newList<string>();@H_502_188@
- ///所有城市id@H_502_188@
- publicList<string>allCityId=///<summary>@H_502_188@
- ///所有城市名字@H_502_188@
- ///</summary>@H_502_188@
- string>allCityName=string>();@H_502_188@
- stringlocalUrl=Application.dataPath+"/XMLFile1.xml";@H_502_188@
- ///加载xml文档@H_502_188@
- ///<returns></returns>@H_502_188@
- staticXmlDocumentReadAndLoadXml()@H_502_188@
- XmlDocumentdoc=newXmlDocument();@H_502_188@
- Debug.Log("加载xml文档");@H_502_188@
- doc.Load(localUrl);@H_502_188@
- returndoc;@H_502_188@
- }@H_502_188@
- ///从本地加载xml并获取所有省的名字@H_502_188@
- ///<paramname="url"></param>@H_502_188@
- string>GetAllProvinceName()@H_502_188@
- List<string>_allProvinceName= XmlDocumentxmlDoc=ReadAndLoadXml();@H_502_188@
- //所有province节点
@H_502_188@ - XmlNodeprovinces=xmlDoc.SelectSingleNode("province");@H_502_188@
- foreach(XmlNodeprovinceinprovinces)@H_502_188@
- XmlElement_province=(XmlElement)province;@H_502_188@
- //所有provinceName添加到列表@H_502_188@
- allProvinceName.Add(_province.GetAttribute("name"));@H_502_188@
- }@H_502_188@
- Debug.Log("所有省数目"+allProvinceName.Count);@H_502_188@
- _allProvinceName=allProvinceName;@H_502_188@
- return_allProvinceName;@H_502_188@
- ///根据当前省ID返回当前省的所有城市名@H_502_188@
- ///<paramname="NowProvinceId"></param>@H_502_188@
- string>GetAllCityNameByNowProvinceId(stringNowProvinceId)@H_502_188@
- List<string>NowAllCityName= XmlDocumentxmlDoc=ReadAndLoadXml();@H_502_188@
- //所有province节点
@H_502_188@ - XmlNodeprovinces=xmlDoc.SelectSingleNode("province");@H_502_188@
- inprovinces)@H_502_188@
- XmlElement_province=(XmlElement)province;@H_502_188@
- //当前城市id@H_502_188@
- if(NowProvinceId==_province.GetAttribute("id"))@H_502_188@
- foreach(XmlElementcityin_province.ChildNodes)@H_502_188@
- XmlElement_city=(XmlElement)city;@H_502_188@
- //当前城市的所有cityName添加到列表@H_502_188@
- NowAllCityName.Add(_city.GetAttribute("name"));@H_502_188@
- returnNowAllCityName;@H_502_188@
- ///根据省的ID返回省的名字@H_502_188@
- ///<paramname="provinceId"></param>@H_502_188@
- ///<returns></returns>@H_502_188@
- stringGetProvinceName(stringprovinceId)@H_502_188@
- string_provinceName="";@H_502_188@
- if(provinceId==_province.GetAttribute("id"))@H_502_188@
- //获取实际省名@H_502_188@
- _provinceName=_province.GetAttribute("name");@H_502_188@
- return_provinceName;@H_502_188@
- ///根据城市ID返会城市名字@H_502_188@
- ///<paramname="cityId"></param>@H_502_188@
- stringGetCityName(stringcityId)@H_502_188@
- stringcityName="";@H_502_188@
- if(_NowProvinceId==_province.GetAttribute("id"))@H_502_188@
- in_province.ChildNodes)@H_502_188@
- XmlElement_city=(XmlElement)city;@H_502_188@
- if(cityId==_city.GetAttribute("id"))@H_502_188@
- //获取实际城市名@H_502_188@
- cityName=_city.GetAttribute("name");@H_502_188@
- returncityName;@H_502_188@
- }@H_502_188@
copy