0
XDocument doc = XDocument.Load(file);

error message "An error occurred while parsing EntityName. Line 90, position 28."

This are the value of Line 90 in XML

<CommercialInvoiceNo>7878 & 7879</CommercialInvoiceNo>

I think the error is because of &

Reynan
  • 163
  • 1
  • 1
  • 11

2 Answers2

3

As already discussed here, the ampersand symbol & itself is not permitted to occur in an XML file; basically this means that the input you are trying to parse is not valid XML. Theoretically you should refuse the input and report an error.

That being said, in the reality of existing software systems, you somehow have to deal with the incorrect input; one possibility would be to change the input by replacing & with the &amp; entity.

Community
  • 1
  • 1
Codor
  • 17,447
  • 9
  • 29
  • 56
1

Well, & is a special symbol and must be encoded:

https://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references

<CommercialInvoiceNo>7878 &amp; 7879</CommercialInvoiceNo>

In case you can't edit XML file, but have to deal with this invalid data you can try to amend it, e.g. with regular expressions:

  String text = File.ReadAllText(@"C:\MyData.xml");

  text = Regex.Replace(source, @"&(\W|$)", match => "&amp;" + match.Value.Substring(1));

  XDocument doc = XDocument.Parse(text);

However, it's just a temporal patch in case you want to read and proceed the invalid file just now.

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215