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)')
Dimitre Novatchev
  • 240,661
  • 26
  • 293
  • 431
Thurein
  • 2,536
  • 7
  • 34
  • 49

2 Answers2

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;
Aliostad
  • 80,612
  • 21
  • 160
  • 208
  • . Thanks for your reply .. Do you have any idea on performance comparison between XmlDocument and XDocument? – Thurein Dec 29 '10 at 17:28
  • For a small XML (less than a few MB) should not be much difference. Have a look here: http://stackoverflow.com/questions/4383919/xdocument-versus-xmldocument – Aliostad Dec 29 '10 at 17:36
0

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

Community
  • 1
  • 1
SwDevMan81
  • 48,814
  • 22
  • 151
  • 184