XPath Nodes

教程

https://www.w3schools.com/xml/xpath_nodes.asp

节点之间的关系

Parent,Children,Siblings,Ancestors,Descendants

 

 qutoric

最新的online工具  http://www.qutoric.com/xslt/analyser/xpathtool.html

旧版的 http://qutoric.com/sketchpath/xpath-editor.html

 

 

visual studio的插件

 https://github.com/uli-weltersbach/XPathTools

https://github.com/uli-weltersbach/XPathTools/wiki 使用方法

Copy value-based XPath

https://github.com/uli-weltersbach/XPathTools/wiki/Copying-XPaths#copy-value-based-xpath

在Distinct XPath中新增一个key,这样可以拿到appSettings下面的key/value的xpath

/configuration/appSettings/add[@key='OutputRuleWebPartExecutionTimeElapse']/@key

 <add key="OutputRuleWebPartExecutionTimeElapse" value="False" />

将鼠标放在add上面,右键,选择copy xpath,然后找到第三个

得到的路径 /configuration/appSettings/add[@key='OutputRuleWebPartExecutionTimeElapse']    

这个node的OuterXml是:<add key="OutputRuleWebPartExecutionTimeElapse" value="False" />

这个node的value是null

 

将鼠标放在value上面,选择copy xpath,然后找到第三个

得到的路径 /configuration/appSettings/add[@key='OutputRuleWebPartExecutionTimeElapse']/@value

这个node的OuterXml是:value="False"

这个node的value是: False

 [Test]
        public void XPathTest()
        {
            var path1 = "/configuration/appSettings/add[@key='OutputRuleWebPartExecutionTimeElapse']";
            var path2 = "/configuration/appSettings/add[@key='OutputRuleWebPartExecutionTimeElapse']/@value";
           
            var desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            var fileName = "test.xml";
            var filePath = Path.Combine(desktopPath, fileName);
            var doc = new XmlDocument();
            doc.Load(filePath);
            var node1 = doc.SelectSingleNode(path1);
            var node2= doc.SelectSingleNode(path2);
            Console.WriteLine(node1.OuterXml);
            Console.WriteLine(node2.OuterXml);
            Console.WriteLine(node2.Value);
        }

 

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <appSettings>
    <add key="OutputRuleWebPartExecutionTimeElapse" value="False" />
  </appSettings>
</configuration>

 

多种方法,通过C#移除xmlnode

https://stackoverflow.com/questions/6500989/deleting-xml-using-a-selected-xpath-and-a-for-loop 

XmlDocument document = new XmlDocument();
document.Load(fileName);

XmlNode node = document.SelectSingleNode("Elements/Element1");
node.ParentNode.RemoveChild(node);

document.Save(fileName);

 

Editing XML Data using XPathNavigator

https://docs.microsoft.com/en-us/dotnet/standard/data/xml/editing-xml-data-using-xpathnavigator 

https://docs.microsoft.com/en-us/dotnet/api/system.xml.xpath.xpathnavigator.select?view=netframework-4.7.2

 

posted @ 2018-04-23 14:58  ChuckLu  阅读(380)  评论(0编辑  收藏  举报