Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.XML.XmlException: ' ' is an unexpected token. The expected token is ';'

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">
like image 909
Stephen Wigginton Avatar asked Jul 29 '13 18:07

Stephen Wigginton


5 Answers

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 &amp; if it was needed.

like image 66
Nicola Avatar answered Nov 16 '22 07:11

Nicola


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" />
like image 24
Kumar Shishir Avatar answered Nov 16 '22 06:11

Kumar Shishir


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.

like image 20
fcuesta Avatar answered Nov 16 '22 08:11

fcuesta


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.

like image 9
Stephen Wigginton Avatar answered Nov 16 '22 06:11

Stephen Wigginton


You can check you document in the w3c validator http://validator.w3.org/

like image 2
Fried Avatar answered Nov 16 '22 07:11

Fried