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

核能气质少年 提交于 2020-01-01 09:23:12

问题


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">

回答1:


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.




回答2:


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.




回答3:


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.




回答4:


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




回答5:


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" />



回答6:


The error is mostly descriptive that your attribute having a name containing spaces and it is not allowed as per XMLparser. You need to remove it to meet xml standards. you should use HTML agility pack [http://htmlagilitypack.codeplex.com/ ] instead.



来源:https://stackoverflow.com/questions/17931723/system-xml-xmlexception-is-an-unexpected-token-the-expected-token-is

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!