0

I am trying to parse an XMl document that i received into a string from a web service call.

String content = ...;//long xml document
using(TextReader reader = new StringReader(content))
using(XmlReader xml_reader = XmlReader.Create(reader, settings))
{
    XML = new XPathDocument(xml_reader);
}

however i get an exception :

An error occurred while parsing EntityName. Line 1, position 1721.

i looked through the document around that character and it was in the middle of a random tag, however about 20-30 chars earlier i noticed that there were unescaped ampersands (& characters), so im thinking that that is the problem.

running:

content.Substring(1700, 100);//results in the following text
"alue>1 time per day& with^honey~&water\\\\</Value></Frequency></Direction>          </Directions>     "
                    ^unescaped & char 1721 is the 'w'

how can i successful read this document as xml?

apaderno
  • 28,547
  • 16
  • 75
  • 90
luke
  • 14,518
  • 4
  • 46
  • 57
  • Report this as a bug to the supplier of this XMI. Their code producing this is wrong and should be fixed. – Don Roby Aug 04 '10 at 22:01

1 Answers1

1

verify that your xml encoding matches theirs (the top of the document, something like <?xml version="1.0" encoding="ISO-8859-9"?>). Substitute the value from the webservice xml document for webserviceEncoding below

using(XmlReader r = XmlReader.Create(new StreamReader(fileName, Encoding.GetEncoding(webserviceEncoding)))) {
    XML = new XPathDocument( r );
    // ... 
}

If that doesn't work

  1. Replace it in the string prior to loading it into an xml parser
  2. Notify the webservice vendor
µBio
  • 10,668
  • 6
  • 38
  • 56
  • just do content.replace("&", "&")? won't that possibly screw up other parts of the document? – luke Aug 04 '10 at 20:14
  • @luke I wouldn't wholesale replace like that. Just replace that one instance. Or you could manually (regex) look at all the contents of the value tags and sanitize them. – µBio Aug 04 '10 at 20:32
  • You get the check because notifying the xml provider was the right solution. sometimes its best not to hack the solution together, thanks – luke Aug 12 '10 at 23:51