C# System.XML.XmlException:“ ”是意外标记。预期的令牌是 ';'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17931723/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
System.XML.XmlException: ' ' is an unexpected token. The expected token is ';'
提问by Stephen Wigginton
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. I've had it export the xml back out to a string, and it looks fine.
pageData 是来自另一个系统的自定义对象,它正在吐出数据流。我已经将 xml 导出回一个字符串,它看起来很好。
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.
当我检查 HTML 上的那一行时,它只是一个元素的结束标记。xml 异常给出的行/位置有多可靠?我只是将表单的源代码转储到 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">
采纳答案by Stephen Wigginton
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 文档加载。
回答by fcuesta
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 Packinstead.
HTML 不同于 XML。XML 的规则比 HTML 严格得多。可能您的 HTML 不是格式良好的 XML。除非您能确保您的 HTML 与 XHTML 兼容,否则您无法使用 XML 解析器解析 HTML。请改用HTML Agility Pack。
回答by Fried
You can check you document in the w3c validator http://validator.w3.org/
您可以在 w3c 验证器http://validator.w3.org/ 中检查您的文档
回答by koolprasad2003
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.
该错误主要是描述您的属性具有包含空格的名称,并且根据 XMLparser 是不允许的。您需要将其删除以符合 xml 标准。您应该改用 HTML 敏捷包 [ http://htmlagilitypack.codeplex.com/]。
回答by Nicola
The issue I had turned out to be an ampersand &
in a URL where a semi-colon ;
did not follow it.
结果我发现问题&
是 URL 中的“&”号后面;
没有跟有分号。
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 &
if it was needed.
幸运的是 URL 不需要在我的 HTML 代码中包含 & 号位,所以我完全删除了它。我想 URL 编码会有所帮助,&
如果需要,将其替换为。
回答by Kumar Shishir
Another thing to look at, XML does not allow HTML attributes without its value.
另一件要注意的事情是,XML 不允许没有值的 HTML 属性。
e.g.;
例如;
<input required name="Entity" />
can't be loaded as XML document and will raise an error as:
无法作为 XML 文档加载,并且会引发如下错误:
'name' is an unexpected token. The expected token is '='.
'name' 是一个意外的标记。预期的标记是“=”。
Hence better to use:
因此更好地使用:
<input required="required" name="Entity" />