HtmlAgilityPack The '"' character, hexadecimal value 0x22, cannot be included in a name

.net html html-agility-pack vb.net

Question

This line:

    Dim NewHTMLString As String = XDocument.Parse(htmldoc.DocumentNode.OuterHtml).ToString()

Produces this error:

    The '"' character, hexadecimal value 0x22, cannot be included in a name.

This is the line in the HTML it says is wrong:

    if ( typeof JSON != 'object' || !JSON.stringify || !JSON.parse ) { document.write( "<scr" + "ipt type=\"text\/javascript\" src=\"http:\/\/blahblah"><\/script>\n" ); };

Accepted Answer

That's because XDocument meant to deal with XML, hence it doesn't support arbitrary Javascript string. XDocument thinks this part : <scr", as beginning of an XML node and double-quote (") character in the XML node name is considered invalid.

I was using XDocument in the answer to your previous question to get beautifully formatted XML output in console, and I did that because I know exactly that my HTML is XML compliant. In this case, your HTML isn't valid from XML point of view and it isn't clear what you're trying to achieve using XDocument here. If you simply need to check result from modification you did to the original HTML, you can either directly print htmldoc.DocumentNode.OuterHtml to console or save the HTML to a new file like so :

htmldoc.Save("path_to_new_file.html")


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why