0

I have a service which returns the below xml as string.I am using Xdocument parse method and XmlDocument load methods to convert the string to xml. but i want to parse and get the status and i_numer which i need to use for further processing.can some one point me in right direction or give some hints.below is the xml i am using.

i tried the innerxml property from the Xdocument and XmlDocument which is returning the whole "" element and this is not what i needed.

<Report>
    <Incidentreport Company="company1" ID="sample">
       <status i_number="12345678" status="sucessful" />
    </Incidentreport>
</Report>
  • Possible duplicate of [Reading XML file in C# with XpathNavigator](http://stackoverflow.com/questions/3769162/reading-xml-file-in-c-sharp-with-xpathnavigator) – cokeman19 Mar 09 '16 at 01:42

2 Answers2

0

The following should work:

string str = [string of xml goes here];
string i_number = string.Empty;
XmlDocument doc = new XmlDocument();
doc.Load(str);
XmlNode node = doc.SelectSingleNode("//status");
i_number = node.Attributes["i_number"].Value;
Maxqueue
  • 2,194
  • 2
  • 23
  • 55
0

You can use SelectSingleNode() which accept XPath parameter to get the target attribute value in one go * :

var raw = @"<Report>
    <Incidentreport Company='company1' ID='sample'>
       <status i_number='12345678' status='sucessful' />
    </Incidentreport>
</Report>";
var doc = new XmlDocument();
doc.LoadXml(raw);
var result = doc.SelectSingleNode("/Report/Incidentreport/status/@i_number");
Console.WriteLine(result.Value);

dotnetfiddle demo

*) notice how XML attribute can be referenced by using @attribute_name syntax in XPath

har07
  • 88,338
  • 12
  • 84
  • 137