C#读取XML文档
下面是一个读取XML文档的比较通用的方法,可以很方便地读取XML文档格式.XML文档如下:
<?xml version="1.0" encoding="utf-8" ?> <RolesRoot ID="0" RoleName="应用权限配置" RoleValue="" RoleUrl="" RoleMark=""> <Roles ID="CLGL" RoleName="车辆管理" RoleValue="" RoleUrl="" RoleMark=""> <Role ID="CLGL_CLXX" RoleName="车辆基本信息" RoleValue="" RoleUrl="车辆管理/车辆基本信息" RoleMark=""> <RoleChild ID="" RoleName="编辑" RoleValue="" RoleUrl="" RoleMark="clbjqx"></RoleChild> <RoleChild ID="" RoleName="修改" RoleValue="" RoleUrl="" RoleMark="clxgqx"></RoleChild> <RoleChild ID="" RoleName="删除" RoleValue="" RoleUrl="" RoleMark="clscqx"></RoleChild> </Role> <Role ID="CLGL_SJXX" RoleName="司机基本信息" RoleValue="" RoleUrl="车辆管理/司机基本信息" RoleMark=""> <RoleChild ID="" RoleName="编辑" RoleValue="" RoleUrl="" RoleMark="sjbjqx"></RoleChild> <RoleChild ID="" RoleName="修改" RoleValue="" RoleUrl="" RoleMark="sjxgqx"></RoleChild> <RoleChild ID="" RoleName="删除" RoleValue="" RoleUrl="" RoleMark="sjscqx"></RoleChild> </Role> </Roles> <Roles ID="HYGL" RoleName="" RoleValue="" RoleUrl="" RoleMark=""> <Role ID="" RoleName="" RoleValue="" RoleUrl="" RoleMark=""></Role> <Role ID="" RoleName="" RoleValue="" RoleUrl="" RoleMark=""></Role> <Role ID="" RoleName="" RoleValue="" RoleUrl="" RoleMark=""></Role> </Roles> </RolesRoot> 读取该文档的C#代码如下所示:
/// <summary> /// 获取XML文档中元素的值 /// </summary> /// <param name="parentNodePath">父级节点位置,如RolesRoot/Roles</param> /// <param name="childNodeName">子节点名称,如要在Role节点下找相关元素值</param> /// <param name="matchElementName">要进行匹配的元素名称,如通过ID元素值来找匹配</param> /// <param name="id">ID元素值</param> /// <param name="elementName">需要获取的元素名称</param> /// <returns></returns> public static string GetXmlElementValue(string parentNodePath, string childNodeName,string matchElementName, string id, string elementName) { string outPut = string.Empty; try { string xmlpath = ConfigurationManager.AppSettings["RolesConfig"]; XmlDocument doc = new XmlDocument(); doc.Load(xmlpath);
XmlNodeList nodes = doc.SelectSingleNode(parentNodePath).ChildNodes; foreach (XmlElement node in nodes) { if (node.Name == childNodeName) { if (node.Attributes[matchElementName].Value == id) { outPut = node.Attributes[elementName].Value; } } } } catch (Exception ex) { Response.Write(ex.Message); } return outPut; } 上面介绍了读取一般格式的XML文档的方法,下面介绍读取特殊XML文件属性的方法,该XML文档定义了ID属性,如下:
<?xml version="1.0" encoding="utf-8" ?> <!DOCTYPE RolesRoot [ <!ELEMENT Role ANY> <!ELEMENT RoleName ANY> <!ELEMENT RoleValue ANY> <!ELEMENT RoleUrl ANY> <!ELEMENT RoleMark ANY> <!ATTLIST Role RoleID ID #REQUIRED> <!ATTLIST RolesRoot RoleID IDREF #REQUIRED> ]>
<RolesRoot RoleID="0" RoleName="应用权限配置" RoleValue="" RoleUrl="" RoleMark=""> <Role RoleID="CLGL" RoleName="车辆管理" RoleValue="" RoleUrl="" RoleMark=""> <Role RoleID="CLGL_CLXX" RoleName="车辆基本信息" RoleValue="" RoleUrl="车辆管理/车辆基本信息" RoleMark=""> <Role RoleID="CLGL_CLXX_BJ" RoleName="编辑" RoleValue="" RoleUrl="" RoleMark="clbjqx"></Role> <Role RoleID="CLGL_CLXX_XG" RoleName="修改" RoleValue="" RoleUrl="" RoleMark="clxgqx"></Role> <Role RoleID="CLGL_CLXX_SC" RoleName="删除" RoleValue="" RoleUrl="" RoleMark="clscqx"></Role> </Role> <Role RoleID="CLGL_SJXX" RoleName="司机基本信息" RoleValue="" RoleUrl="车辆管理/司机基本信息" RoleMark=""> <Role RoleID="" RoleName="编辑" RoleValue="" RoleUrl="" RoleMark="sjbjqx"></Role> <Role RoleID="" RoleName="修改" RoleValue="" RoleUrl="" RoleMark="sjxgqx"></Role> <Role RoleID="" RoleName="删除" RoleValue="" RoleUrl="" RoleMark="sjscqx"></Role> </Role> </Role> </RolesRoot>我们通过ID属性读取该XML文档,获取元素的值.
/// <summary> /// 通过ID获取元素值 /// </summary> /// <param name="id">XML文档中定义的唯一ID属性</param> /// <param name="elementName">对应ID元素下要查找的元素名称</param> /// <returns></returns> public static string GetXmlElementValueById(string id, string elementName) { string outPut = string.Empty; try { string xmlpath = ConfigurationManager.AppSettings["RolesConfig"]; XmlDocument doc = new XmlDocument(); doc.Load(xmlpath);
XmlElement elem = doc.GetElementById(id); outPut = elem.Attributes[elementName].Value; } catch (Exception ex) { Response.Write(ex.Message); } return outPut; }
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/xuwenwu/archive/2008/05/26/2482863.aspx
|
请发表评论