Matt3o
Matt3o

Reputation: 407

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

i have an XML like

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

how can i improve this c# code snippet

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

to get only the shape node with attribute Name "Pluto" whose parent have the attribute Tag "Pippo"?

Upvotes: 1

Views: 482

Answers (1)

Andrey Korneyev
Andrey Korneyev

Reputation: 26846

You can get this node using following Xpath string:

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

Upvotes: 4

Related Questions