Linq-to-XML not getting content of a node that contains html tags
I have an XML file that I'm trying to parse with Linq-to-XML. One o开发者_如何学Pythonf 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?
.Value
means 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:
<image>
<img>/Images/m1cznk4a6fh7.jpg
</img>
</image>
Then your code would work.
You'll have to go further in your decendents to the <img/>
tag and then get the .Value
of attribute src
to retrieve the text you need.
If you're going to be storing HTML inside the XML elements it should be inside a <![CDATA[]]>
comment so that LINQ2XML knows not to treat it as additional XML markup.
<image><![CDATA[<img src="Images/abc.jpg />]]></image>
If memory serves, you shouldn't have to do anything special to extract the value without the CDATA comment wrapping it, but you may need to call a property other than Value
. I don't quite recall.
That is because there is no Value
nested under Image
only another element (img
). You would need to do something like:
XElement imgElement = document.Descendants("image").SingleOrDefault().FirstNode;
Then access the Value
property to get src
. Otherwise, if you are looking for the img
tag as plain text you would need to save it in your XML doc as a CDATA section e..g
<image><![CDATA[<img src="/Images/m1cznk4a6fh7.jpg" />]]></image>
精彩评论