1
votes

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

1 Answers

4
votes

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);