【问题标题】:Escaping new-line characters with XmlDocument使用 XmlDocument 转义换行符
【发布时间】:2011-03-01 21:33:56
【问题描述】:

我的应用程序使用 XmlDocument 生成 XML。部分数据包含换行符和回车符。

当像这样将文本分配给 XmlElement 时:

   e.InnerText = "Hello\nThere";

生成的 XML 如下所示:

<e>Hello
There</e>

XML 的接收者(我无法控制)将换行符视为空白,并将上述文本视为:

 "Hello There"

为了让接收者保留换行符,它需要编码为:

<e>Hello&#xA;There</e>

如果数据应用于 XmlAttribute,则换行符被正确编码。

我尝试使用 InnerText 和 InnerXml 将文本应用于 XmlElement,但两者的输出相同。

有没有办法让 XmlElement 文本节点以其编码形式输出换行符和回车符?

这里有一些示例代码来演示这个问题:

string s = "return[\r] newline[\n] special[&<>\"']";
XmlDocument d = new XmlDocument();
d.AppendChild( d.CreateXmlDeclaration( "1.0", null, null ) );
XmlElement  r = d.CreateElement( "root" );
d.AppendChild( r );
XmlElement  e = d.CreateElement( "normal" );
r.AppendChild( e );
XmlAttribute a = d.CreateAttribute( "attribute" );
e.Attributes.Append( a );
a.Value = s;
e.InnerText = s;
s = s
    .Replace( "&" , "&amp;"  )
    .Replace( "<" , "&lt;"   )
    .Replace( ">" , "&gt;"   )
    .Replace( "\"", "&quot;" )
    .Replace( "'" , "&apos;" )
    .Replace( "\r", "&#xD;"  )
    .Replace( "\n", "&#xA;"  )
;
e = d.CreateElement( "encoded" );
r.AppendChild( e );
a = d.CreateAttribute( "attribute" );
e.Attributes.Append( a );
a.InnerXml = s;
e.InnerXml = s;
d.Save( @"C:\Temp\XmlNewLineHandling.xml" );

这个程序的输出是:

<?xml version="1.0"?>
<root>
  <normal attribute="return[&#xD;] newline[&#xA;] special[&amp;&lt;&gt;&quot;']">return[
] newline[
] special[&amp;&lt;&gt;"']</normal>
  <encoded attribute="return[&#xD;] newline[&#xA;] special[&amp;&lt;&gt;&quot;']">return[
] newline[
] special[&amp;&lt;&gt;"']</encoded>
</root>

提前致谢。 克里斯。

【问题讨论】:

  • 你想要什么不清楚。请展示你想要什么,你得到什么
  • 查看前 4 行代码:我从什么开始,我得到什么,接收者看到什么,我想要什么。属性以我想要的方式编码换行符,元素不会。

标签: c# xml .net-2.0 xmldocument


【解决方案1】:

HttpUtility.HtmlEncode()怎么样?
http://msdn.microsoft.com/en-us/library/73z22y6h.aspx

好的,很抱歉那里的错误线索。 HttpUtility.HtmlEncode()不会处理您面临的换行问题。

不过,此博客链接将帮助您
http://weblogs.asp.net/mschwarz/archive/2004/02/16/73675.aspx

基本上,换行处理由xml:space="preserve" 属性控制。

示例工作代码:

XmlDocument doc = new XmlDocument();
doc.LoadXml("<ROOT/>");
doc.DocumentElement.InnerText = "1234\r\n5678";

XmlAttribute e = doc.CreateAttribute(
    "xml", 
    "space", 
    "http://www.w3.org/XML/1998/namespace");
e.Value = "preserve";
doc.DocumentElement.Attributes.Append(e);

var child = doc.CreateElement("CHILD");
child.InnerText = "1234\r\n5678";
doc.DocumentElement.AppendChild(child);

Console.WriteLine(doc.InnerXml);
Console.ReadLine();

输出将显示:

<ROOT xml:space="preserve">1234
5678<CHILD>1234
5678</CHILD></ROOT>

【讨论】:

  • 我已经对此进行了测试,但我的接收器无法识别或处理 xml:space 属性。新行肯定必须编码为 或者它们被转换为空白。
【解决方案2】:

编码可能是使用methods described here 的最佳选择。或者,也许您可​​以考虑使用 CData section 代替您的内容。

【讨论】:

  • 在我上面的示例代码中,有一个简单的编码器。问题是让 XmlElement 保留编码字符。它不断将它们转换回换行符和回车符。
【解决方案3】:

在 .net 2.0 中使用 XmlDocument PreserveWhitespace 开关

XmlDocument d = new XmlDocument();
d.PreserveWhitespace = true;

【讨论】:

  • 当我尝试这个时,它对换行符的编码没有影响。
【解决方案4】:

我有同样的问题 Preserve carriage returns when i write/read from xml file using asp.net

解决方法是在生成html后将xml空间替换为html空间 我加了这个

        strHtml = strHtml.Replace("&lt;br/&gt;", "<br/>");

在方法结束之前关闭流阅读器

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多