0

I have this string structured as XML:

<damp fullmedia="">
  <mediaitem>
    <img id="1119" version="29a5623f-d41c-4418-97b7-3cea8579ffd8" parentid="1052" level="2" writerid="0" nodetype="1032" template="0" sortorder="2" createdate="2016-11-30T10:09:40" updatedate="2016-11-30T10:09:40" nodename="2016-11-25_1251411957-277" urlname="2016-11-25_1251411957-277" writername="admin" nodetypealias="Image" path="-1,1052,1119">
      <umbracofile>/media/1013/2016-11-25_1251411957-277.jpg</umbracofile>
      <umbracowidth>2598</umbracowidth>
      <umbracoheight>1732</umbracoheight>
      <umbracobytes>350613</umbracobytes>
      <umbracoextension>jpg</umbracoextension>    
    </img>
  </mediaitem>
</damp>

I want to extract the value of property "umbracofile", so I did this:

var stringAsXml = "<damp fullmedia="">....";
var xmlDoc = new XmlDocument();
var imageUrl = xmlDoc.SelectSingleNode("//mediaItem/img/umbracoFile").InnerText;

The code works fine but I have the error "Root element is missing". As I just want to parse and extract a node value, how can I get rid of this error?

4
  • The code works fine but I have the error I'd argue that these statements are actually contradictory. Error might be related to fact that you are looking for node Image when the node is actually img Commented Jan 17, 2017 at 15:24
  • Sorry, my mistake, the node find /img/ not /Image/ Commented Jan 17, 2017 at 15:32
  • Oh, your img tag is also never closed. I don't know if that's a typo Commented Jan 17, 2017 at 15:33
  • It was a typo, I fixed it, sorry! Commented Jan 17, 2017 at 15:50

1 Answer 1

1

The code works fine but I have the error "Root element is missing".

Then the code doesn't work that fine I guess? This should work:

var stringAsXml = "<damp fullmedia=\"\"><mediaitem><img id=\"1119\" version=\"29a5623f-d41c-4418-97b7-3cea8579ffd8\" parentid=\"1052\" level=\"2\" writerid=\"0\" nodetype=\"1032\" template=\"0\" sortorder=\"2\" createdate=\"2016-11-30T10:09:40\" updatedate=\"2016-11-30T10:09:40\" nodename=\"2016-11-25_1251411957-277\" urlname=\"2016-11-25_1251411957-277\" writername=\"admin\" nodetypealias=\"Image\" path=\"-1,1052,1119\"><umbracofile>/media/1013/2016-11-25_1251411957-277.jpg</umbracofile><umbracowidth>2598</umbracowidth><umbracoheight>1732</umbracoheight><umbracobytes>350613</umbracobytes><umbracoextension>jpg</umbracoextension></img></mediaitem></damp>";
var xmlDoc = new XmlDocument();
xmlDoc.LoadXml(stringAsXml);
var imageUrl = xmlDoc.SelectSingleNode("damp/mediaitem/img/umbracofile").InnerText;

And this as well (if you have several <umbracofile> nodes and want to select the first one):

var imageUrl = xmlDoc.SelectSingleNode("//umbracofile").InnerText;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.