根据XPath找到某一个特定的节点
2010-06-04 17:38
260 查看
工作中需要解析到XML文件,发现XPath很神奇。如果能灵活设置XPath,找到某个特定的节点就可以轻而易举了。
下面的链接大家可以参考一下,主要就是介绍XPath的。
来自MSDN的介绍:http://msdn.microsoft.com/en-us/library/ms256086.aspx
便于大家理解,还可以看看下边的两个链接:
http://www.zvon.org/xxl/XPathTutorial/Output/examples.html
http://www.w3schools.com/xpath/xpath_syntax.asp
下面的代码是以后需要继续维护的XmlHelper类,先放到这里。
代码
下面的链接大家可以参考一下,主要就是介绍XPath的。
来自MSDN的介绍:http://msdn.microsoft.com/en-us/library/ms256086.aspx
便于大家理解,还可以看看下边的两个链接:
http://www.zvon.org/xxl/XPathTutorial/Output/examples.html
http://www.w3schools.com/xpath/xpath_syntax.asp
下面的代码是以后需要继续维护的XmlHelper类,先放到这里。
代码
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml; namespace XmlHelper { class XmlHelper { public static void CreateXML(string xml,string fileName) { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(xml); xmlDoc.Save(fileName); } public static void CreateXML(string fileName, string node, string version, string encoding, string standalone) { XmlDocument xmlDoc = new XmlDocument(); XmlDeclaration xmlDeclaration = xmlDoc.CreateXmlDeclaration(version, encoding, standalone); xmlDoc.AppendChild(xmlDeclaration); XmlNode root = xmlDoc.CreateElement(node); xmlDoc.AppendChild(root); xmlDoc.Save(fileName); } public static void CreateXML(string fileName,XmlNode node,string version, string encoding, string standalone ) { XmlDocument xmlDoc = new XmlDocument(); XmlDeclaration xmlDeclaration = xmlDoc.CreateXmlDeclaration(version, encoding, standalone); xmlDoc.AppendChild(xmlDeclaration); xmlDoc.AppendChild(node); xmlDoc.Save(fileName); } /// <summary> /// Find xml by xpath according to subelement value /// </summary> /// <param name="xml"></param> /// <param name="xPath">/students/student/name["may"]</param> /// <returns></returns> public static string FindNodeXmlByXPathWithSubElementValue(string fileName, string xPath) { string outerXml = string.Empty; XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(fileName); XmlNode node = xmlDoc.SelectSingleNode(xPath); outerXml = node.OuterXml; return outerXml; } /// <summary> /// Find specified node by xpath according to subelement value /// </summary> /// <param name="xml"></param> /// <param name="xPath">/students/student/name["may"]</param> /// <returns></returns> public static XmlNode FindNodeByXPathWithSubElementValue(string fileName, string xPath) { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(fileName); XmlNode node = xmlDoc.SelectSingleNode(xPath); return node; } } }
相关文章推荐
- C#使用xpath找到一个节点
- 给一个二叉树,每个节点都是正或负整数,如何找到一个子树,它所有节点的和最大?
- 跑ssis分组差错:没有关联“”。假设无法找到一个特定的连接元件,Connections 这种错误发生的收集
- 【FAQ】如何根据commit id找到对应节点所属的分支和标签?
- 在一个较长一维数组中找到特定值索引的捷径(matlab)
- 【数据结构】给出一个链表,遍历一次就找到中间节点
- 如何在二叉树中找到一个节点的后继节点
- 输入一个链表,找到倒数第K个节点。使得只扫描一次链表。
- 如何在未知链表中找到一个中间节点
- 根据XPATH去查看修改xml文件节点的内容
- 写了一个上下级关系,根据自己所在部门的编号,找到下级,不包括自己
- winform中根据节点名称找到节点
- JavaScript找到一个元素的父节点
- 找到两个数的差为一个特定数
- Objective-c 根据某一个时区的时间获取特定时区的时间
- 节点利用js的Node 遍历找到repeater的一个字段 实例
- OSG中找到特定节点的方法
- 递归调用(id,pid,name)根据一个id获取它所有的子节点
- 20140719 找到单链表的倒数第K个节点 判断一个链表是否成为一个环形 反转
- 在二叉树中找到一个节点的后继节点