An error occurred while parsing EntityName

2019-04-12 11:24发布

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

标签: c# xml parsing
2条回答
乱世女痞
2楼-- · 2019-04-12 12:04

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.

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;
}
查看更多
Luminary・发光体
3楼-- · 2019-04-12 12:04

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.

Try beefing up your question with more details to get a more definitive answer.

查看更多
登录 后发表回答