An error occurred while parsing the EntityName

I am trying to load an XML document into an XPathDocument object in C #. This line is included in my xml documents: 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 the character’s normal reason is “é”. Does anyone know how I can avoid this error ... My idea is to insert entity declarations into the XML document and after replacing all special characters with entities ... but this is a long time and I'm not sure if it works . Do you have any other ideas? Simpler? Many thanks

+3
source share
2 answers

I was going to publish this, and that's when the servers went down. I think I correctly rewrote it from memory:

I think the problem is that by default it XPathDocumentuses XmlTextReaderto analyze the contents of the supplied file, and this one XmlTextReaderuses the parameter EntityHandling ExpandEntities.

In other words, when you rely on the default settings, it XmlTextReaderchecks the input XML and tries to resolve all entities. This is best done manually, with full control 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;
}
+6
source

This is usually caused by a mismatch between the encoding used to read the file and the actual encoding of the files.

I guess I would say that the file is encoded in UTF-8, but you are reading it with the default encoding.

, .

+4

Source: https://habr.com/ru/post/1705641/


All Articles