System.XML.XmlException: '' - unexpected token. Expected Token: ';'

I have an HTML form that I am trying to load using XDocument.Load and I get the following error:

' ' is an unexpected token. The expected token is ';'. Line 1257, position 66. at System.Xml.XmlTextReaderImpl.Throw(Exception e) 

The code simply calls the following:

 XDocument xmlDoc = XDocument.Load(pageData.Stream); 

pageData is a custom object from another system that spills out a stream of data. I exported xml back to the string, and it looks fine.

When I check this line for HTML, it is just the closing tag for the element. How reliable is the line / position given by xml exception? I just dump the form source into notepad ++ for validation, and I don't see that this will be a problem.

EDIT: Below are the first few lines before and after the error. I marked a line with an error.

  </p> </td> </tr> </table> </td> </tr> <----Error Line <tr> <td> <div id="BusinessJustificationForm"> <table id="BusinessJustificationTable"> <tr> <td class="seperator" colspan="7"> 
+4
source share
5 answers

This problem was caused by the Name attribute, which has a name that contains spaces. As soon as I looked through all this and decided that I was able to load the HTML as an XML document.

+6
source

The problem I ran into was the ampersand & in the url where the semicolon was not specified ; .

For instance:

 <a href="http://www.something.com?id=123&name=456"></a> 

Fortunately, the url should not have an ampersand bit in my HTML code, so I completely deleted it. I guess URL encoding will help by replacing it with &amp; if necessary.

+7
source

HTML is different from XML. XML has much stricter rules than HTML. Your HTML is probably not well-formed XML. If you cannot verify that your HTML is compatible with XHTML, you cannot parse HTML with an XML parser. Use the HTML Agility Pack instead.

+5
source

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

+2
source

The error is mainly described by the fact that your attribute has a name containing spaces, and it is not resolved according to XMLparser. You need to remove it to comply with xml standards. you must use the HTML flexibility package [ http://htmlagilitypack.codeplex.com/ ].

-one
source

Source: https://habr.com/ru/post/1494120/


All Articles