C# 使用XmlDocument转义新行字符
我的应用程序使用XmlDocument生成XML。某些数据包含换行符和回车符 将文本分配给如下XmlElement时:C# 使用XmlDocument转义新行字符,c#,xml,.net-2.0,xmldocument,C#,Xml,.net 2.0,Xmldocument,我的应用程序使用XmlDocument生成XML。某些数据包含换行符和回车符 将文本分配给如下XmlElement时: e.InnerText = "Hello\nThere"; <e>Hello There</e> 生成的XML如下所示: e.InnerText = "Hello\nThere"; <e>Hello There</e> 为使接收器保留新行,要求编码为: <e>Hello
There&
e.InnerText = "Hello\nThere";
<e>Hello
There</e>
生成的XML如下所示:
e.InnerText = "Hello\nThere";
<e>Hello
There</e>
为使接收器保留新行,要求编码为:
<e>Hello
There</e>
Hello
;那里
如果将数据应用于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( "&" , "&" )
.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" );
string s=“return[\r]换行符[\n]特殊[&\']”;
XmlDocument d=新的XmlDocument();
d、 AppendChild(d.CreateXmlDeclaration(“1.0”,null,null));
XmlElement r=d.CreateElement(“根”);
d、 儿童(r);
XmlElement e=d.CreateElement(“正常”);
r、 儿童(e);
xmldattribute a=d.CreateAttribute(“属性”);
e、 属性。附加(a);
a、 值=s;
e、 InnerText=s;
s=s
.替换(“&”、“&;”)
.替换(“,”)
.替换(“\”,“”)
.替换(“'”、“&apos;”)
.替换(“\r”、“
;”)
.替换(“\n”、“xA;”)
;
e=d.CreateElement(“编码”);
r、 儿童(e);
a=d.CreateAttribute(“属性”);
e、 属性。附加(a);
a、 InnerXml=s;
e、 InnerXml=s;
d、 保存(@“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>
如何使用HttpUtility.HtmlEncode()
?
好的,很抱歉出现了错误的线索。HttpUtility.HtmlEncode()
将不处理您面临的换行问题
不过,此博客链接将为您提供帮助
基本上,换行符处理由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();
XmlDocument doc=新的XmlDocument();
doc.LoadXml(“”);
doc.DocumentElement.InnerText=“1234\r\n5678”;
XmlAttribute e=doc.CreateAttribute(
“xml”,
“空间”,
"http://www.w3.org/XML/1998/namespace");
e、 Value=“保存”;
doc.DocumentElement.Attributes.Append(e);
var child=doc.CreateElement(“child”);
child.InnerText=“1234\r\n5678”;
doc.DocumentElement.AppendChild(子级);
Console.WriteLine(doc.InnerXml);
Console.ReadLine();
输出将为:
<ROOT xml:space="preserve">1234
5678<CHILD>1234
5678</CHILD></ROOT>
1234
56781234
5678
编码可能是使用的最佳选择。或者,您也可以考虑在内容中使用a。在.net 2.0中使用XmlDocument开关
我也有同样的问题
解决方案是在生成html后将xml空间替换为html空间
我加上这个
strHtml = strHtml.Replace("<br/>", "<br/>");
strHtml=strHtml.Replace(“br/”,“
”);
在关闭流读取器之前的方法结束时当我尝试此操作时,它对新行字符的编码没有影响。我已经进行了测试,我的接收器无法识别或处理xml:space属性。新行肯定必须编码为
;或者转换为空白。在我上面的示例代码中,有一个简单的编码器。问题是让XmlElement保留编码字符。它不断地将它们转换回新行和回车字符。您想要什么还不清楚。请说明您想要什么,以及您得到了什么查看前4行代码:我从什么开始,我得到了什么,接收者看到了什么,我想要什么颂词按照我想要的方式编码新行,元素则不然。