在解析实体名称出现错误 [英] An error occurred while parsing EntityName

查看:160
本文介绍了在解析实体名称出现错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一个XML文档加载到在C#中的对象的XPathDocument。
我的XML文件包括此行:
的Trésdégagée+ RADE
,当解析器到达那里它给了我这个错误:
$ B $,而解析实体名称出现错误 b口知道这就是字符E正常的原因。有谁知道我怎么能避免这个错误......我的想法是插入到XML文档中的实体申报后实体替换所有的特殊字符...但它的长,我不知道,如果它的工作。你有其他的想法?更简单呢?
非常感谢

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

推荐答案

正要张贴此就在这时,该服务器发生故障。我想我已经正确地改写它从内存中:

我认为问题在于这个事实中,默认情况下的XPathDocument 使用了的XmlTextReader 来解析提供的文件的内容和的XmlTextReader 使用了 EntityHandling > ExpandEntities

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.

在换句话说,当你依靠的默认设置,一个的XmlTextReader 将验证输入XML并尝试解决所有实体。更好的办法是通过充分的控制权 XmlReaderSettings (我总是做手工)手动执行此操作:

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):

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;
}

这篇关于在解析实体名称出现错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆