加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 无法将字符串转换为XML

发布时间:2020-12-15 23:24:40 所属栏目:百科 来源:网络整理
导读:我将 XML作为字符串传递给方法,并再次将其转换为XML来完成我的工作. 它的工作正常,但是当有特殊字符时,如或=它给出了一个错误. 我的XML字符串: SuggestedReadings Suggestion Text="Customer Centricity" Link="http://wdp.wharton.upenn.edu/book/customer
我将 XML作为字符串传递给方法,并再次将其转换为XML来完成我的工作.

它的工作正常,但是当有特殊字符时,如&或=它给出了一个错误.

我的XML字符串:

<SuggestedReadings>
   <Suggestion Text="Customer Centricity" Link="http://wdp.wharton.upenn.edu/book/customer-centricity/?utm_source=Coursera&utm_medium=Web&utm_campaign=custcent" SuggBy="Pete Fader?s" />
   <Suggestion Text="Global Brand Power" Link="http://wdp.wharton.upenn.edu/books/global-brand-power/?utm_source=Coursera&utm_medium=Web&utm_campaign=glbrpower" SuggBy="Barbara Kahn?s" />
</SuggestedReadings>

我的代码是:

public class saveData(string strXml)
{
      XmlDocument xmlDoc = new XmlDocument();
      xmlDoc.LoadXml(CD.SRList);// here its giving error
}

错误:

‘=’ is an unexpected token. The expected token is ‘;’. Line 1,position 150.

完整错误是:

System.Xml.XmlException was unhandled by user code HResult=-2146232000
Message=’=’ is an unexpected token. The expected token is ‘;’. Line 1,
position 150. Source=System.Xml LineNumber=1 LinePosition=150
SourceUri=”” StackTrace: at
System.Xml.XmlTextReaderImpl.Throw(Exception e) at
System.Xml.XmlTextReaderImpl.Throw(String res,String[] args) at
System.Xml.XmlTextReaderImpl.ThrowUnexpectedToken(String
expectedToken1,String expectedToken2) at
System.Xml.XmlTextReaderImpl.ThrowUnexpectedToken(Int32 pos,String
expectedToken1,String expectedToken2) at
System.Xml.XmlTextReaderImpl.HandleEntityReference(Boolean
isInAttributeValue,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.XmlTextReaderImpl.Read() at
System.Xml.XmlLoader.LoadNode(Boolean skipOverWhitespace) at
System.Xml.XmlLoader.LoadDocSequence(XmlDocument parentDoc) at
System.Xml.XmlLoader.Load(XmlDocument doc,XmlReader reader,Boolean
preserveWhitespace) at System.Xml.XmlDocument.Load(XmlReader
reader) at System.Xml.XmlDocument.LoadXml(String xml) at
ICA.LMS.Service.Controllers.AdminCourseApiController.SaveCourse(CourseDetails
CD) in d:Live
ProjectsICA_LMSICA_LMS_WebAPIControllersAdminCourseApiController.cs:line
122 at lambda_method(Closure,Object,Object[] ) at
System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass10.b__9(Object
instance,Object[] methodParameters) at
System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object
instance,Object[] arguments) at
System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext
controllerContext,IDictionary`2 arguments,CancellationToken
cancellationToken) InnerException:

解决方法

您的文档缺少XML标头,这是必需的.此外,你没有正确地逃避&字符.

尝试在XML文档之上添加它:

<?xml version="1.0" encoding="UTF-8"?>

并且还替换&与& amp ;. (见list of characters to escape)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读