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?
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;