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

今天碰到的一个问题,把string 解析成xml时,抛出异常,如下:

问题:

An exception of type'System.Xml.XmlException' occurred and was caught.

Type : System.Xml.XmlException,System.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken= 

Message : '=' is an unexpected token. The expected token is ';'. Line X,position XXX.

或者 

Message : The 'SPAN' start tag on line 1position 137 does not match the end tag of 'P'. Line X, position XXX.

Source : System.Xml

Help link :

LineNumber : X

LinePosition : XXX

SourceUri :

Data :System.Collections.ListDictionaryInternal

TargetSite : Void Throw(System.String,System.String[]) HResult : -2146232000

Stack Trace :    atSystem.Xml.XmlTextReaderImpl.Throw(String res, String[] args)

  at System.Xml.XmlTextReaderImpl.HandleEntityReference(BooleanisInAttributeValue, EntityExpandType expandType, Int32& charRefEndPos)

  at System.Xml.XmlTextReaderImpl.ParseAttributeValueSlow(Int32 curPos,Char quoteChar, NodeData attr)

  at System.Xml.XmlTextReaderImpl.ParseAttributes()

  at System.Xml.XmlTextReaderImpl.ParseElement()

  at System.Xml.XmlTextReaderImpl.ParseElementContent()

  at System.Xml.Linq.XContainer.ReadContentFrom(XmlReader r)

  at System.Xml.Linq.XContainer.ReadContentFrom(XmlReader r, LoadOptionso)

  at System.Xml.Linq.XElement.ReadElementFrom(XmlReader r, LoadOptions o)

  at System.Xml.Linq.XElement.Load(XmlReader reader, LoadOptions options)

  at System.Xml.Linq.XElement.Parse(String text, LoadOptions options)


字符串是

<P><B>title</B></P>

<P> text 1 </P>

<P><B>description:</B></P>

<P>1. Provide<A href="http://dfafa&daffa&fasdf=1">rr</A></P>

<P>2. Reference <A href="http://dfafa&daffa&fasdf=1">ee</A>Guideline </P>

<P>3. Discuss</P>

<P>4. Schedulet </P>


原因是

A 标签在解析过程有问题,说是因为&符号。 

或者 HTML 标签不匹配, 即:开始和结束标签不匹配。



解决方法:

1.针对特殊字符,例如:&, 需要转义

2.对于标签不匹配,例如: <span> 没有结束标签,需要手动修改。 

可以使用工具XML Notepad, 来确定出错位置,很方便。


如果在开发过程中,需要使用文本编辑器。

推荐一个工具:TinyMCE. 

这个工具可以自动修复不正确的标签。出错率低。








  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值