逃跑的新行字符的XmlDocument [英] Escaping new-line characters with XmlDocument
问题描述
我的应用程序中使用的XmlDocument生成一个XML。一些数据包含换行符和回车符
My application generates XML using XmlDocument. Some of the data contains newline and carriage return characters.
当文本被分配到一个XmlElement的是这样的:
When text is assigned to an XmlElement like this:
e.InnerText = "Hello\nThere";
生成的XML看起来是这样的:
The resulting XML looks like this:
<e>Hello
There</e>
的
XML(我没有控制权)的接收器把新线为空白并看到上面的文字为:
The receiver of the XML (which I have no control over) treats the new-line as white space and sees the above text as:
"Hello There"
有关接收方保留新线它需要的编码为:
For the receiver to retain the new-line it requires the encoding to be:
<e>Hello
There</e>
如果数据被施加到一个XmlAttribute,新线被正确编码
If the data is applied to an XmlAttribute, the new-line is properly encoded.
我试着将文本使用的InnerText和InnerXml的XmlElement但是输出上是相同的,以
I've tried applying text to XmlElement using InnerText and InnerXml but the output is the same for both.
有没有?的方式来获得的XmlElement文本节点输出的新行和回车符号在其编码的形式
Is there a way to get XmlElement text nodes to output new-lines and carriage-returns in their encoded forms?
下面是一些示例代码来演示问题:
Here is some sample code to demonstrate the problem:
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( "&" , "&" )
.Replace( "<" , "<" )
.Replace( ">" , ">" )
.Replace( "\"", """ )
.Replace( "'" , "'" )
.Replace( "\r", "
" )
.Replace( "\n", "
" )
;
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[
] newline[
] special[&<>"']">return[
] newline[
] special[&<>"']</normal>
<encoded attribute="return[
] newline[
] special[&<>"']">return[
] newline[
] special[&<>"']</encoded>
</root>
提前
感谢。
克里斯。
Thanks in advance. Chris.
推荐答案
<击>如何使用 HttpUtility.HtmlEncode()?code>结果
的http:// MSDN。 microsoft.com/en-us/library/73z22y6h.aspx 击>
确定,很抱歉的错误领导那里。 HttpUtility.HtmlEncode()
将不可以处理你所面临的新行问题。
OK, sorry about the wrong lead there. HttpUtility.HtmlEncode()
will not handle the newline issue you're facing.
这个博客链接将会帮助你,尽管
的 http://weblogs.asp.net/mschwarz/archive/2004/02/16/73675.aspx
This blog link will help you out, though
http://weblogs.asp.net/mschwarz/archive/2004/02/16/73675.aspx
基本上,换行符处理由 XML控制:空间=保存
属性
Basically, the newline handling is controlled by the xml:space="preserve"
attribute.
样品工作代码:
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>
这篇关于逃跑的新行字符的XmlDocument的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!