[英]XmlDocument class fails to load a file that contains a node with C# code
I have an xml file with a node that contains some c# code. 我有一个XML文件,其节点包含一些C#代码。
<Script Name="WrapText">
var sb = new System.Text.StringBuilder();
int lastSpaceIndex = 0;
for(int i = 0; i < paragraph.length; i++)
{
var curChar = paragraph[i];
sb.Append(curChar);
if (System.Char.IsWhiteSpace(curChar))
{
lastSpaceIndex = i;
}
if (i % splitlength == 0)
{
if (lastSpaceIndex != 0)
{
sb[lastSpaceIndex] = '\n';
}
}
}
return sb.ToString();
</Script>
when i try to load this using an XmlDocument and XmlReader classes in C# like this: 当我尝试使用C#中的XmlDocument和XmlReader类加载此文件时,如下所示:
XmlReader xReader = XmlReader.Create(new MemoryStream(ASCIIEncoding.UTF8.GetBytes(imml)), _ReaderSettings); XmlDocument xDoc = new XmlDocument(); xDoc.Load(xReader);
I get this error: 我收到此错误:
Name cannot begin with the ' ' character, hexadecimal value 0x20. Line 25, position 21.
If I remove the for loop line, it loads the file without any problems. 如果删除for循环行,它将加载文件而没有任何问题。
Why is this happening? 为什么会这样呢?
Creation of XML by hand/with string concatenation always causes such problems: you <
is not properly encoded and as result < paragraph.length; i++)...
手工创建/使用字符串连接创建XML总是会导致这样的问题:您
<
编码不正确,因此< paragraph.length; i++)...
长度< paragraph.length; i++)...
< paragraph.length; i++)...
is treated as element name. < paragraph.length; i++)...
被视为元素名称。
You should encode all special characters as required. 您应该根据需要对所有特殊字符进行编码。 (ie
<
for <
). (即
<
表示<
)。 See http://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references for compact list. 有关紧凑列表,请参见http://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.