0

First of all, sorry if this question is already answered, but i haven't found it.

I have an XML file that looks like this:

<data success="1" status="200">
    <id>SbBGk</id>
    <title/>
    <description/>
    <datetime>1341533193</datetime>
    <type>image/jpeg</type>
    <animated>false</animated>
    <width>2559</width>
    <height>1439</height>
    <size>521916</size>
    <views>1</views>
    <bandwidth>521916</bandwidth>
    <deletehash>eYZd3NNJHsbreD1</deletehash>
    <section/>
    <link>http://i.imgur.com/SbBGk.jpg</link>
</data>

I want to get the id as a string. Any ideas?

Note: The XML file is a web response, that i retrieve using:

XmlDocument doc = new XmlDocument();
byte[] response = w.UploadValues("https://api.imgur.com/3/upload.xml", values);
string xml = Encoding.UTF8.GetString(response);
doc.LoadXml(xml);
Neuron
  • 5,141
  • 5
  • 38
  • 59
Charlystar
  • 23
  • 7
  • Look into Xpath see : https://msdn.microsoft.com/en-us/library/d271ytdx(v=vs.110).aspx & https://msdn.microsoft.com/en-us/library/ms256086(v=vs.110).aspx – Jon P Jun 01 '17 at 23:14
  • 2
    Ditch `XmlDocument`. It's old and has been superseded by `XDocument`. Once you've got a valid `XDocument`, `var id=(string)doc.Root.Element("id");` will do you. – spender Jun 01 '17 at 23:16
  • Consider using [`XDocument.Load()`](https://msdn.microsoft.com/en-us/library/cc838321(v=vs.110).aspx). – Dour High Arch Jun 01 '17 at 23:16
  • Changed to `XDocument` and `var id=(string)doc.Root.Element("id");` worked! Thanks – Charlystar Jun 01 '17 at 23:22
  • Nice explanation XDocument over XMLDocument https://stackoverflow.com/questions/1542073/xdocument-or-xmldocument – Mrunalini Aug 28 '18 at 13:30

1 Answers1

0

Instead of using XMLDocument, use XDocument.

XDocument doc = new XDocument();
doc.Load(xml);
string id = doc.Root.Element("id").Value;