Skip to main content
 首页 » 编程设计

c#之XPath : Get nodes where child node contains an attributeHow to select node that have and attribute and whose parent have another attribute in c#

2024年02月06日20arxive

我有一个类似的 XML

<Validation> 
    <Presentation> 
        <Slide Tag = "Pippo"> 
            <Shape Name = "Pluto"/> 
        </Slide> 
        <Shape Name = "Pluto"/> 
    </Presentation> 
</Validation> 

我怎样才能改进这个 C# 代码片段

 String xPath = string.Format("/Validation/Presentation/Shape[@Name='{0}'][1]", "Pluto"); 
 XmlNode node = doc.DocumentElement.SelectSingleNode(xPath); 

只获取属性名称为“Pluto”的形状节点,其父节点的属性标签为“Pippo”?

请您参考如下方法:

您可以使用以下 Xpath 字符串获取此节点:

string xPath = string.Format("//*[@Tag='{0}']/Shape[@Name='{1}']", "Pippo","Pluto"); 
XmlNode node = doc.DocumentElement.SelectSingleNode(xPath);