An error occurred while parsing EntityName

22,746

Solution 1

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

Solution 2

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.

Share:
22,746
user61652
Author by

user61652

Updated on July 09, 2022

Comments

  • user61652
    user61652 almost 2 years

    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