LINQ to XML-将CDATA渲染为HTML
作者:互联网
我有以下XML:
<stories>
<story id="1234">
<title>This is a title</title>
<date>1/1/1980</date>
<article>
<![CDATA[<p>This is an article.</p>]]>
</article>
</story>
</stories>
以及下面的Linq to C#中的XML代码:
@{
XDocument xmlDoc = XDocument.Load("foo.xml");
var stories = from story in xmlDoc.Descendants("stories")
.Descendants("story")
.OrderByDescending(s => (string)s.Attribute("id"))
select new
{
title = story.Element("title").Value,
date = story.Element("date").Value,
article = story.Element("article").Value,
};
foreach (var story in stories)
{
<text><div class="news_item">
<span class="title">@story.title</span>
<span class="date">@story.date</span>
<div class="story">@story.article</div>
</div></text>
}
}
呈现的HTML如下输出到浏览器:
<div class="news_item">
<span class="title">This is a title</span>
<span class="date">1/1/1980</span>
<div class="story"><p>This is an article.</p></div>
</div>
我想要< p>在浏览器中呈现为HTML的代码,未进行编码.我该如何完成?
解决方法:
剃刀默认编码值.您需要使用Html.Raw助手来避免它(Html.Raw() in ASP.NET MVC Razor view)
<div class="story">@Html.Raw(story.article)</div>
标签:razor,linq-to-xml,asp-net,c 来源: https://codeday.me/bug/20191031/1977269.html