XML 问题 - 节点内的 HTML 被删除(ASP.NET C# LINQ to XML)

发布于 2024-11-06 09:48:15 字数 2292 浏览 0 评论 0原文

当我加载此 XML 节点时,节点内的 HTML 被完全删除。

这是我用来获取节点内的值的代码,该值是与 HTML 结合的文本:

var stuff = innerXml.Descendants("root").Elements("details").FirstOrDefault().Value;

在“details”节点内部是如下所示的文本:

"This is <strong>test copy</strong>. This is <a href="#">A Link</a>"

当我查看“stuff”var 时,我看到这个:

"This is test copy. This is A Link". There is no HTML in the output... it is pulled out.

Maybe Value should be是innerXml还是innerHtml? FirstOrDefault() 与此有什么关系吗?

我不认为 xml 需要“cdata”块...

这是一个更完整的代码片段:

 announcements =
                from link in xdoc.Descendants(textContainer).Elements(textElement)
                where link.Parent.Attribute("id").Value == Announcement.NodeId
                select new AnnouncmentXml
                {
                    NodeId = link.Attribute("id").Value,
                    InnerXml = link.Value
                };

XDocument innerXml;
innerXml = XDocument.Parse(item.InnerXml);
var abstract = innerXml.Descendants("root").Elements("abstract").FirstOrDefault().Value;

最后,这是 Xml 节点的片段。请注意标准 xml 结构中如何存在“InnerXml”。它开始于 .我将其称为“InnerXml”,这就是我传递到名为 InnerXml 的 XDocument 中的内容:

 <text id="T_403080"><root> <title>How do I do stuff?</title> <details> Look Here <a href="http://" target=" _blank">Some Form</a>. Please note that lorem ipsum dlor sit amet.</details> </root></text> 

[更新]

我尝试使用此帮助程序 lamda,它将返回 HTML,但它会被转义,因此当它显示在页面上时,我查看视图中的实际 HTML(它显示而不是给出链接,而是将标签打印到屏幕上:

Title = innerXml.Descendants("root").Elements("title").FirstOrDefault().Nodes().Aggregate(new System.Text.StringBuilder(), (sb, node) => sb.Append(node.ToString()), sb => sb.ToString());

所以我尝试了 HTMLEncode 和 HTMLDecode 但都没有帮助。一个在屏幕上显示转义字符,另一个在屏幕上显示转义字符什么也没做:

Title = 
                        System.Web.HttpContext.Current.Server.HtmlDecode(
                            innerXml.Descendants("root").Elements("details").Nodes().Aggregate(new System.Text.StringBuilder(), (sb, node) => sb.Append(node.ToString()), sb => sb.ToString())
                        );

When I load this XML node, the HTML within the node is being completely stripped out.

This is the code I use to get the value within the node, which is text combined with HTML:

var stuff = innerXml.Descendants("root").Elements("details").FirstOrDefault().Value;

Inside the "details" node is text that looks like this:

"This is <strong>test copy</strong>. This is <a href="#">A Link</a>"

When I look in "stuff" var I see this:

"This is test copy. This is A Link". There is no HTML in the output... it is pulled out.

Maybe Value should be innerXml or innerHtml? Does FirstOrDefault() have anything to do with this?

I don't think the xml needs a "cdata" block...

HEre is a more complete code snippet:

 announcements =
                from link in xdoc.Descendants(textContainer).Elements(textElement)
                where link.Parent.Attribute("id").Value == Announcement.NodeId
                select new AnnouncmentXml
                {
                    NodeId = link.Attribute("id").Value,
                    InnerXml = link.Value
                };

XDocument innerXml;
innerXml = XDocument.Parse(item.InnerXml);
var abstract = innerXml.Descendants("root").Elements("abstract").FirstOrDefault().Value;

Finally, here is a snippet of the Xml Node. Notice how there is "InnerXml" within the standard xml structure. It starts with . I call this the "InnerXml" and this is what I am passing into the XDocument called InnerXml:

 <text id="T_403080"><root> <title>How do I do stuff?</title> <details> Look Here <a href="http://" target=" _blank">Some Form</a>. Please note that lorem ipsum dlor sit amet.</details> </root></text> 

[UPDATE]

I tried to use this helper lamda, and it will return the HTML but it is escaped, so when it displays on the page I see the actual HTML in the view (it shows instead of giving a link, the tag is printed to screen:

Title = innerXml.Descendants("root").Elements("title").FirstOrDefault().Nodes().Aggregate(new System.Text.StringBuilder(), (sb, node) => sb.Append(node.ToString()), sb => sb.ToString());

So I tried both HTMLEncode and HTMLDecode but neither helped. One showed the escaped chars on the screen and the other did nothing:

Title = 
                        System.Web.HttpContext.Current.Server.HtmlDecode(
                            innerXml.Descendants("root").Elements("details").Nodes().Aggregate(new System.Text.StringBuilder(), (sb, node) => sb.Append(node.ToString()), sb => sb.ToString())
                        );

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

所有深爱都是秘密 2024-11-13 09:48:15

我最终使用 XmlDocument 而不是 XDocument。 LINQ to XML 似乎还不够成熟,无法支持我想要做的事情。 XDoc 没有 InnerXml 属性,只有 Value。

也许有一天我将能够恢复到 LINQ。现在,我只需要把这件事从我的盘子里拿出来。这是我的解决方案:

// XmlDoc to hold custom Xml within each node
        XmlDocument innerXml = new XmlDocument();
        try
        {
            // Parse inner xml of each item and create objects
            foreach (var faq in faqs)
            {
                innerXml.LoadXml(faq.InnerXml);

                FAQ oFaq = new FAQ();

                #region Fields
                // Get Title value if node exists and is not null
                if (innerXml.SelectSingleNode("root/title") != null)
                {
                    oFaq.Title = innerXml.SelectSingleNode("root/title").InnerXml;
                }

                // Get Details value if node exists and is not null
                if (innerXml.SelectSingleNode("root/details") != null)
                {
                    oFaq.Description = innerXml.SelectSingleNode("root/details").InnerXml;
                }
                #endregion

                result.Add(oFaq);
            }
        }
        catch (Exception ex)
        {
            // Handle Exception
        } 

I ended up using an XmlDocument instead of an XDocument. It doesn't seem like LINQ to XML is mature enough to support what I am trying to do. THere is no InnerXml property of an XDoc, only Value.

Maybe someday I will be able to revert to LINQ. For now, I just had to get this off my plate. Here is my solution:

// XmlDoc to hold custom Xml within each node
        XmlDocument innerXml = new XmlDocument();
        try
        {
            // Parse inner xml of each item and create objects
            foreach (var faq in faqs)
            {
                innerXml.LoadXml(faq.InnerXml);

                FAQ oFaq = new FAQ();

                #region Fields
                // Get Title value if node exists and is not null
                if (innerXml.SelectSingleNode("root/title") != null)
                {
                    oFaq.Title = innerXml.SelectSingleNode("root/title").InnerXml;
                }

                // Get Details value if node exists and is not null
                if (innerXml.SelectSingleNode("root/details") != null)
                {
                    oFaq.Description = innerXml.SelectSingleNode("root/details").InnerXml;
                }
                #endregion

                result.Add(oFaq);
            }
        }
        catch (Exception ex)
        {
            // Handle Exception
        } 
宫墨修音 2024-11-13 09:48:15

我确实认为将详细信息节点包装在 cdata 块中是正确的决定。 CData 基本上表明其中包含的信息应被视为文本,而不是解析 XML 特殊字符。详细信息节点中的 html 字符,尤其是 <<和>与 XML 规范直接冲突,确实应该标记为文本。

您也许可以通过获取innerXml 来解决此问题,但如果您可以控制文档内容,则cdata 是正确的决定。

如果您需要一个示例来了解其外观,这里是详细节点的修改版本:

<details>
    <![CDATA[
         This is <strong>test copy</strong>. This is <a href="#">A Link</a>
    ]]>
</details>

I do think wrapping your details node in a cdata block is the right decision. CData basically indicates that the information contained within it should be treated as text, and not parsed for XML special characters. The html charaters in the details node, especially the < and > are in direct conflict with the XML spec, and should really be marked as text.

You might be able to hack around this by grabbing the innerXml, but if you have control over the document content, cdata is the correct decision.

In case you need an example of how that should look, here's a modified version of the detail node:

<details>
    <![CDATA[
         This is <strong>test copy</strong>. This is <a href="#">A Link</a>
    ]]>
</details>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文