简体   繁体   English

System.XML.XmlException:“ ”是意外标记。 预期的令牌是 ';'

[英]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:我有一个 HTML 表单,我正在尝试使用 XDocument.Load 加载它,但收到以下错误:

' ' 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. pageData 是来自另一个系统的自定义对象,它正在吐出数据流。 I've had it export the xml back out to a string, and it looks fine.我已经将 xml 导出回一个字符串,它看起来很好。

When i check that line on the HTML, it's just a closing tag for an element.当我检查 HTML 上的那一行时,它只是一个元素的结束标记。 How reliable is the line/position given by the xml exception? xml 异常给出的行/位置有多可靠? I'm just dumping the source of the form into notepad++ for validation, and i can't see that it would be an issue.我只是将表单的源代码转储到 notepad++ 中进行验证,我看不出这会是一个问题。

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 ;我原来的问题是一个&符号&在一个分号的 URL 中; 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.幸运的是,URL 不需要在我的 HTML 代码中包含与号位,因此我将其完全删除。 I guess URL encoding would help, replacing it to &amp;我想 URL 编码会有所帮助,将其替换为&amp; if it was needed.如果需要的话。

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.一旦我完成了整个事情并解决了这个问题,我就能够将 HTML 作为 XML 文档加载。

HTML is different from XML. HTML 不同于 XML。 XML has much more strict rules than HTML. XML 的规则比 HTML 严格得多。 Probably your HTML is not well-formed XML.可能您的 HTML 不是格式良好的 XML。 Unless you can ensure that your HTML is XHTML compliant, you can not parse HTML with an XML parser.除非您能确保您的 HTML 与 XHTML 兼容,否则您无法使用 XML 解析器解析 HTML。 Use HTML Agility Pack instead.请改用HTML Agility Pack

您可以在 w3c 验证器http://validator.w3.org/ 中检查您的文档

Another thing to look at, XML does not allow HTML attributes without its value.另一件要注意的事情是,XML 不允许没有值的 HTML 属性。

eg;例如;

<input required name="Entity" />

can't be loaded as XML document and will raise an error as:无法作为 XML 文档加载,并将引发如下错误:

'name' is an unexpected token. 'name' 是一个意外的标记。 The expected token is '='.预期的标记是“=”。

Hence better to use:因此更好地使用:

<input required="required" name="Entity" />

The error is mostly descriptive that your attribute having a name containing spaces and it is not allowed as per XMLparser.该错误主要是描述您的属性具有包含空格的名称,并且根据 XMLparser 是不允许的。 You need to remove it to meet xml standards.您需要将其删除以符合 xml 标准。 you should use HTML agility pack [ http://htmlagilitypack.codeplex.com/ ] instead.您应该改用 HTML 敏捷包 [ http://htmlagilitypack.codeplex.com/ ]。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 System.Xml.XmlException:“[值]”是意外标记。 预期的标记是 '"' 或 ''' - System.Xml.XmlException: '[value]' is an unexpected token. The expected token is '"' or ''' 流异常&System.Xml.XmlException:&#39;-&#39;是意外令牌。 预期令牌为” - Stream exception & System.Xml.XmlException: '--' is an unexpected token. The expected token is '' RestSharp Methods抛出System.Xml.XMlException“=”是一个意外的令牌。 预期的标记是&#39;;&#39; - RestSharp Methods Throw System.Xml.XMlException “=” is an unexpected token. The expected token is ';' XmlException: &#39;”&#39; 是一个意外的标记。 预期的标记是 &#39;&quot;&#39; 或 &#39;&#39;&#39; - XmlException: '”' is an unexpected token. The expected token is '"' or ''' 未处理XMLException&#39;var&#39;是意外令牌。 预期令牌为“ =” - XMLException was unhandled 'var' is an unexpected token. The expected token is '=' 未处理XMLException&#39;/&#39;是意外令牌。 预期令牌为“ =”。 4号线,位置18 - XMLException was unhandled '/' is an unexpected token. The expected token is '='. Line 4, position 18 Windows Identity Foundation System.Xml.XmlException:意外的文件结尾 - Windows Identity Foundation System.Xml.XmlException: Unexpected end of file 如何捕获system.xml.xmlexception - How to catch system.xml.xmlexception System.Xml.XmlException-根元素丢失 - System.Xml.XmlException - Root element is missing W8电话System.Xml.XmlException& - W8 Phone System.Xml.XmlException &
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM