I have an HTML form that i'm trying to load using XDocument.Load, and i'm receiving the following error:
' ' is an unexpected token. The expected token is ';'. Line 1257, position 66.
at System.Xml.XmlTextReaderImpl.Throw(Exception e)
The code is just calling the following:
XDocument xmlDoc = XDocument.Load(pageData.Stream);
pageData is a custom object from another system, that is spitting out a datastream. I've had it export the xml back out to a string, and it looks fine.
When i check that line on the HTML, it's just a closing tag for an element. How reliable is the line/position given by the xml exception? I'm just dumping the source of the form into notepad++ for validation, and i can't see that it would be an issue.
EDIT: The following is the first few lines before and after the error. I've marked the error line.
</p>
</td>
</tr>
</table>
</td>
</tr> <----Error Line
<tr>
<td>
<div id="BusinessJustificationForm">
<table id="BusinessJustificationTable">
<tr>
<td class="seperator" colspan="7">
The issue I had turned out to be an ampersand &
in a URL where a semi-colon ;
did not follow it.
For example:
<a href="http://www.something.com?id=123&name=456"></a>
Fortunately the URL did not need to have the ampersand bit in my HTML code so I removed it altogether. I guess URL encoding would help, replacing it to &
if it was needed.
Another thing to look at, XML does not allow HTML attributes without its value.
e.g.;
<input required name="Entity" />
can't be loaded as XML document and will raise an error as:
'name' is an unexpected token. The expected token is '='.
Hence better to use:
<input required="required" name="Entity" />
HTML is different from XML. XML has much more strict rules than HTML. Probably your HTML is not well-formed XML. Unless you can ensure that your HTML is XHTML compliant, you can not parse HTML with an XML parser. Use HTML Agility Pack instead.
This issue was caused by a "Name" attribute having a name containing spaces. Once i went through the whole thing and resolved that, I was able to load the HTML as an XML document.
You can check you document in the w3c validator http://validator.w3.org/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With