I have an XML file that I’m trying to parse with Linq-to-XML. One of the nodes contains a bit of HTML, that I cannot retrieve.
The XML resembles:
<?xml version="1.0" encoding="ISO-8859-1"?>
<root>
<image><img src="/Images/m1cznk4a6fh7.jpg" /></image>
<contentType>Banner</contentType>
</root>
The code is:
XDocument document = XDocument.Parse(content.XML);
XElement imageElement = document.Descendants("image").SingleOrDefault();
image = imageElement.Value; // Doesn't get the content, while if I specify .Descendants("contentType") it works
Any ideas?
.Valuemeans any text within a tag and any child tags, but you don’t have any. When you parsed it,<img/>was viewed as an XML tag, not specific for HTML (Linq doesn’t know the difference). For example, if you had your XML written as:Then your code would work.
You’ll have to go further in your decendents to the
<img/>tag and then get the.Valueof attributesrcto retrieve the text you need.