-1
<Customer>
    <Type H="General Information" ID="GeneralInfo">
         <Row>
             <C H="Customer Name">Mr. Robert</C>
             <C H="Relation">S/O. John</C>
             <C H="Date of Birth">01/01/1985</C>
        </Row>
    </Type>
    <Type H = "Other Details" ID = "ShareDet">
         <Row>
            <C H = "Address 1">XYZ</C>
            <C H = "Address 2">ABC</C>
        </Row>
    </Type>
</Customer>

I am trying to read "Mr. Robert" from above XML in C# but i could not. I tried below Code:

XmlDocument objXmlMain = new XmlDocument();
objXmlMain.LoadXml("Loading_Above_XMLSTRING");
string test = objXmlMain.SelectSingleNode("Customer/Type/Row/C/@H").Value;

I am getting result as "Customer Name" (That is attribute value). I want to read the Name by checking attribute value "Customer Name" and i should get result as "Mr. Robert"

ViKu
  • 235
  • 2
  • 14
  • 2
    Possible duplicate of [XPath: How to select a node by its attribute?](https://stackoverflow.com/questions/1068210/xpath-how-to-select-a-node-by-its-attribute) – Selvin Dec 05 '18 at 12:53
  • or any results of internet search with `c# xpath by attribute name` – Selvin Dec 05 '18 at 12:54

1 Answers1

1

You need to use:

string test = objXmlMain.SelectSingleNode("Customer/Type/Row/C[@H='Customer Name']").Value;

The Customer/Type/Row/C/@H xpath query selects the H attribute itself and .Value returns the text of that attribute.

Ivaylo Slavov
  • 8,839
  • 12
  • 65
  • 108