C# 解析 EntityName 时出错
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/700686/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
An error occurred while parsing EntityName
提问by user61652
I'm trying to load a xml document into an object XPathDocument in C#. My xml documents include this line: trés dégagée + rade and when the parser arrives there it gives me this error: "An error occurred while parsing EntityName" I know that's normal cause of the character "é". Does anybody know how can I avoid this error... My idea is to insert into the xml document an entities declaration and after replace all special characters with entities...but it's long and I'm not sure if it's working. Do you have other ideas? Simpler? Thanks a lot
我正在尝试将 xml 文档加载到 C# 中的对象 XPathDocument 中。我的 xml 文档包括这一行:trés dégagée + rade,当解析器到达那里时,它给了我这个错误:“解析 EntityName 时发生错误”我知道这是字符“é”的正常原因。有谁知道我怎样才能避免这个错误......我的想法是在xml文档中插入一个实体声明,然后用实体替换所有特殊字符......但它很长,我不确定它是否有效。你有其他想法吗?更简单?非常感谢
采纳答案by Cerebrus
Was about to post this and just then the servers went down. I think I've rewritten it correctly from memory:
正要发布这个,就在那时服务器宕机了。我想我已经从记忆中正确地重写了它:
I think that the problem lies within the fact that by default the XPathDocument
uses an XmlTextReader
to parse the contents of the supplied file and this XmlTextReader
uses an EntityHandling
setting of ExpandEntities
.
我认为,问题的关键在于这样的事实中,默认情况下XPathDocument
使用的XmlTextReader
解析提供的文件的内容和XmlTextReader
使用的EntityHandling
设定ExpandEntities
。
In other words, when you rely on the default settings, an XmlTextReader
will validate the input XML and try to resolve all entities. The better way is to do this manually by taking full control over the XmlReaderSettings
(I always do it manually):
换句话说,当您依赖默认设置时,XmlTextReader
将验证输入的 XML 并尝试解析所有实体。更好的方法是通过完全控制XmlReaderSettings
(我总是手动执行)来手动执行此操作:
string myXMLFile = "SomeFile.xml";
string fileContent = LoadXML(myXMLFile);
private string LoadXML(string xml)
{
XPathDocument xDoc;
XmlReaderSettings xrs = new XmlReaderSettings();
// The following line does the "magic".
xrs.CheckCharacters = false;
using (XmlReader xr = XmlReader.Create(xml, xrs))
{
xDoc = new XPathDocument(xr);
}
if (xDoc != null)
{
XPathNavigator xNav = xDoc.CreateNavigator();
return xNav.OuterXml;
}
else
// Unable to load file
return null;
}
回答by AnthonyWJones
Typically this is caused by a mismatch between the encoding used to read the file and the files actually encoding.
通常,这是由用于读取文件的编码与实际编码的文件之间的不匹配引起的。
At a guess I would say the file is UTF-8 encoded but you are reading it with a default encoding.
猜测我会说该文件是 UTF-8 编码的,但您正在使用默认编码读取它。
Try beefing up your question with more details to get a more definitive answer.
尝试用更多细节来加强你的问题,以获得更明确的答案。