ora*_*dov 7 html c# xml linq-to-xml
我正在尝试使用Linq to XML来保存和检索XML文件和Windows窗体应用程序之间的一些HTML.当它将其保存到XML文件时,HTML标记将获得xml编码,并且不会保存为直接HTML.
示例HTML:
<P><FONT color=#004080><U>Sample HTML</U></FONT></P>
Run Code Online (Sandbox Code Playgroud)
保存在XML文件中:
<P><FONT color=#004080><U>Sample HTML</U></FONT></P>
Run Code Online (Sandbox Code Playgroud)
当我手动编辑XML文件并输入所需的HTML时,Linq会拉入HTML并正确显示它.
以下是将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;
Run Code Online (Sandbox Code Playgroud)
我假设这是发生了xml编码的b/c,但我不知道如何处理它.这个问题给了我一些线索......但没有答案(对我而言,至少).
谢谢您的帮助,
奥兰
设置Value属性将自动编码html字符串.这应该可以解决问题,但您需要确保HTML是有效的XML(XHTML).
currentReport.Element("recomendations").ReplaceNodes(XElement.Parse(myCallreports.Comments));
Run Code Online (Sandbox Code Playgroud)
编辑:您可能需要将用户输入的HTML包装在<div> </div>标签中.XElement.Parse期望找到至少包含起始和结束xml标记的字符串.所以,这可能会更好:
currentReport.Element("recomendations").ReplaceNodes(XElement.Parse("<div>" + myCallreports.Comments + "</div>"));
Run Code Online (Sandbox Code Playgroud)
然后你只需要确保像这样的标签<br>被发送<br />.
编辑2:另一个选项是使用XML CDATA.使用<![CDATA[和包装HTML ]]>,但我从未实际使用过,我不确定它如何影响读取xml.
currentReport.Element("recomendations").ReplaceNodes(XElement.Parse("<![CDATA[" + myCallreports.Comments + "]]>"));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6523 次 |
| 最近记录: |