1

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"?

1 Answer 1

4

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);
Sign up to request clarification or add additional context in comments.

2 Comments

Is it equivalent to xPath = string.Format("/Validation/Presentation/Slide[@Tag='{0}']/Shape[@Name='{1}'][1]", "Pippo", "Pluto"); ?
@Matt3o it is not. Your query selects Shape having name Pluto and parent /Validation/Presentation/Slide with tag Pippo. My query selects exactly as it was stated in your question: "Shape node with attribute Name "Pluto" whose parent have the attribute Tag "Pippo" - i.e. parent can be any node having this tag, not only /Validation/Presentation/Slide.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.