1

What is the best way to get node value by using XPath API in C#?

<employee nric="S100" name="Mike" ... />

In T-SQL, the following will give the result:

select xml.value('(/employee/@nric)[1]','nvarchar(max)')

2 Answers 2

2

Using an XmlDocument:

        string s = "<employee nric=\"S100\" name=\"Mike\"  />";
        XmlDocument doc = new XmlDocument();
        doc.LoadXml(s);
        string value = doc.SelectSingleNode("//employee/@nric").Value;
2
  • . Thanks for your reply .. Do you have any idea on performance comparison between XmlDocument and XDocument?
    – Thurein
    Dec 29, 2010 at 17:28
  • For a small XML (less than a few MB) should not be much difference. Have a look here: stackoverflow.com/questions/4383919/…
    – Aliostad
    Dec 29, 2010 at 17:36
0

Select Nodes Using XPath Navigation will be a good start. Then Xpath and selecting a single node

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.