English 中文(简体)
Linq-to-XML没有获得含有html标签的 no子的内容
原标题:Linq-to-XML not getting content of a node that contains html tags

我有XML档案,Im试图与Linq-to-XML教。 其中一个节点载有我无法检索的超文本。

XML类似:

<?xml version="1.0" encoding="ISO-8859-1"?>
<root>
<image><img src="/Images/m1cznk4a6fh7.jpg"  /></image>
<contentType>Banner</contentType>
</root>

该守则是:

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

任何想法?

最佳回答

<代码>。 数值是指在标签和任何儿童标签内的任何案文,但你没有。 当您对它进行分类时,<img/>被视为一种XML 标签,并非专用于超文本(Linq t 知道差异)。 例如,如果你写了XML:

<image>
    <img>/Images/m1cznk4a6fh7.jpg
    </img>
</image>

然后,你的法典将发挥作用。

页: 1 页: 1

问题回答

如果你在XML要素内重新储存超文本,则应在<![CDATA[]>评论中,以便LINQ2XML知道不将其作为额外XML标记处理。

<image><![CDATA[<img src="Images/abc.jpg />]]></image>

如果记忆发挥作用,你就不必做任何特殊的事情来提取价值,而没有《非洲贸易协定》对总结这一价值作出评论,但你可能需要把除<代码”以外的财产称作“。 数值。 我不记得。

这是因为没有<代码>Value,在<代码>Image下印章,只有另一个要素(img)。 你们需要做这样的事情:

XElement imgElement = document.Descendants("image").SingleOrDefault().FirstNode;

然后进入<代码>Value 财产,以获取src。 否则,如果你将<代码>im tag作为便捷文本,那么你就必须在您的XML doc中将其作为CDATA节,例如。

<image><![CDATA[<img src="/Images/m1cznk4a6fh7.jpg" />]]></image>




相关问题
how to represent it in dtd?

I have two element action and guid. guid is a required field when action is add. but when action is del it will not appear in file. How to represent this in dtd ?

.Net application configuration add xml-data

I need to add xml-content to my application configuration file. Is there a way to add it directly to the appSettings section or do I need to implement a configSection? Is it possible to add the xml ...

XStream serializing collections

I have a class structure that I would like to serialize with Xstream. The root class contains a collection of other objects (of varying types). I would like to only serialize part of the objects that ...

MS Word splits words in its XML format

I have a Word 2003 document saved as a XML in WordProcessingML format. It contains few placeholders which will be dynamically replaced by an appropriate content. But, the problem is that Word ...

Merging an XML file with a list of changes

I have two XML files that are generated by another application I have no control over. The first is a settings file, and the second is a list of changes that should be applied to the first. Main ...

How do I check if a node has no siblings?

I have a org.w3c.dom.Node object. I would like to see if it has any other siblings. Here s what I have tried: Node sibling = node.getNextSibling(); if(sibling == null) return true; else ...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

热门标签