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

 

作者:Chuck Lu    GitHub    
posted @   ChuckLu  阅读(382)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2015-04-23 从泛型类中继承
点击右上角即可分享
微信分享提示