I am trying to use Linq to XML to save & retrieve some HTML between an XML file and a windows forms application. When it saves it to the XML file the HTML tags get xml encoded and it isn't saved as straight HTML.
我正在尝试使用Linq to XML在XML文件和windows窗体应用程序之间保存和检索一些HTML。当它保存到XML文件时,HTML标记得到XML编码,而不是直接保存为HTML。
Example HTML:
示例HTML:
<P><FONT color=#004080><U>Sample HTML</U></FONT></P>
Saved in XML File:
保存在XML文件:
<P><FONT color=#004080><U>Sample HTML</U></FONT></P>
When I manually edit the XML file and put in the desired HTML the Linq pulls in the HTML and displays it properly.
当我手工编辑XML文件并放入所需的HTML时,Linq会拉入HTML并正确地显示它。
Here is the code that saves the HTML to the XML file:
下面是将HTML保存到XML文件的代码:
XElement currentReport = (from item in callReports.Descendants("callReport")
where (int)item.Element("localId") == myCallreports.LocalId
select item).FirstOrDefault();
currentReport.Element("studio").Value = myCallreports.Studio;
currentReport.Element("visitDate").Value = myCallreports.Visitdate.ToShortDateString();
// *** The next two XElements store the HTML
currentReport.Element("recomendations").Value = myCallreports.Comments;
currentReport.Element("reactions").Value = myCallreports.Ownerreaction;
I assume this is happening b/c of xml encoding but I am not sure how to deal with it. This question gave me some clues...but no answer (for me, at least).
我假设这是xml编码的b/c,但我不知道如何处理它。这个问题给了我一些线索……但没有回答(至少对我来说)。
Thanks for the help,
谢谢你的帮助,
Oran
奥兰
2 个解决方案
#1
5
Setting the Value property will automatically encode the html string. This should do the trick, but you'll need to make sure that your HTML is valid XML (XHTML).
设置Value属性将自动对html字符串进行编码。这应该可以达到目的,但是您需要确保您的HTML是有效的XML (XHTML)。
currentReport.Element("recomendations").ReplaceNodes(XElement.Parse(myCallreports.Comments));
Edit: You may need to wrap the user entered HTML in <div> </div>
tags. XElement.Parse
expects to find a string with at least a start and end xml tag. So, this might work better:
编辑:您可能需要将用户输入的HTML封装在
标签中。XElement。Parse希望找到一个至少有开始和结束xml标记的字符串。所以,这可能会更好:currentReport.Element("recomendations").ReplaceNodes(XElement.Parse("<div>" + myCallreports.Comments + "</div>"));
Then you just have to make sure that tags like <br>
are being sent in as <br />
.
然后,您只需确保像
这样的标签被作为
发送进来。
Edit 2: The other option would be use XML CDATA. Wrap the HTML with <![CDATA[
and ]]>
, but I've never actually used that and I'm not sure how it affects reading the xml.
编辑2:另一个选项是使用XML CDATA。用and,但我从未真正使用过它,我不确定它是如何影响读取xml的。
currentReport.Element("recomendations").ReplaceNodes(XElement.Parse("<![CDATA[" + myCallreports.Comments + "]]>"));
#2
-1
Try using currentReport.Element("studio").InnerXml
instead of currentReport.Element("studio").Value
试着用currentReport.Element(“工作室”)。InnerXml代替currentReport.Element value(“工作室”)
#1
5
Setting the Value property will automatically encode the html string. This should do the trick, but you'll need to make sure that your HTML is valid XML (XHTML).
设置Value属性将自动对html字符串进行编码。这应该可以达到目的,但是您需要确保您的HTML是有效的XML (XHTML)。
currentReport.Element("recomendations").ReplaceNodes(XElement.Parse(myCallreports.Comments));
Edit: You may need to wrap the user entered HTML in <div> </div>
tags. XElement.Parse
expects to find a string with at least a start and end xml tag. So, this might work better:
编辑:您可能需要将用户输入的HTML封装在
标签中。XElement。Parse希望找到一个至少有开始和结束xml标记的字符串。所以,这可能会更好:currentReport.Element("recomendations").ReplaceNodes(XElement.Parse("<div>" + myCallreports.Comments + "</div>"));
Then you just have to make sure that tags like <br>
are being sent in as <br />
.
然后,您只需确保像
这样的标签被作为
发送进来。
Edit 2: The other option would be use XML CDATA. Wrap the HTML with <![CDATA[
and ]]>
, but I've never actually used that and I'm not sure how it affects reading the xml.
编辑2:另一个选项是使用XML CDATA。用and,但我从未真正使用过它,我不确定它是如何影响读取xml的。
currentReport.Element("recomendations").ReplaceNodes(XElement.Parse("<![CDATA[" + myCallreports.Comments + "]]>"));
#2
-1
Try using currentReport.Element("studio").InnerXml
instead of currentReport.Element("studio").Value
试着用currentReport.Element(“工作室”)。InnerXml代替currentReport.Element value(“工作室”)