Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法将字符串转换为XML_C#_Asp.net_Xml - Fatal编程技术网

C# 无法将字符串转换为XML

C# 无法将字符串转换为XML,c#,asp.net,xml,C#,Asp.net,Xml,我将XML作为字符串传递给一个方法,并再次将其转换为XML来完成我的工作 它的工作正常,但当出现特殊字符,如&或=时,它给出了一个错误 我的XML字符串: <SuggestedReadings> <Suggestion Text="Customer Centricity" Link="http://wdp.wharton.upenn.edu/book/customer-centricity/?utm_source=Coursera&utm_medium=Web&a

我将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>
错误:

“=”是意外标记。所需的标记为“;”。第1行,位置150

完全错误为:

System.Xml.XmlException未由用户代码HResult=-2146232000处理 消息=“=”是意外标记。所需的标记为“;”。第1行, 位置150。Source=System.Xml LineNumber=1 LinePosition=150 SourceUri=”“StackTrace:at System.Xml.XmlTextReaderImpl.Throw(异常e)位于 System.Xml.XmlTextReaderImpl.Throw(字符串res,字符串[]args)位于 System.Xml.XmlTextReaderImpl.ThrowUnexpectedToken(字符串 expectedToken1,字符串expectedToken2)位于 System.Xml.XmlTextReaderImpl.ThrowUnnexpectedToken(Int32 pos,字符串 expectedToken1,字符串expectedToken2)位于 System.Xml.XmlTextReaderImpl.HandleEntityReference(布尔值 isInAttributeValue、EntityExpandType expandType、Int32和charRefEndPos) 在System.Xml.XmlTextReaderImpl.ParseAttributeValueSlow(Int32 curPos, 字符quoteChar,NodeData attr)在 System.Xml.XmlTextReaderImpl.ParseAttributes()位于 System.Xml.XmlTextReaderImpl.ParseElement()位于 System.Xml.XmlTextReaderImpl.ParseElementContent()位于 System.Xml.XmlTextReaderImpl.Read()位于 位于的System.Xml.XmlLoader.LoadNode(布尔skipOverWhitespace) System.Xml.XmlLoader.LoadDocSequence(XmlDocument parentDoc)位于 System.Xml.xmloader.Load(XmlDocument文档,XmlReader阅读器,布尔值 在System.Xml.XmlDocument.Load(XmlReader)中保留空白 在System.Xml.XmlDocument.LoadXml(字符串Xml)处 ICA.LMS.Service.Controllers.AdminCourseApiController.SaveCourse(CoursedDetails CD)在d:\Live中 Projects\ICA\u LMS\ICA\u LMS\u WebAPI\Controllers\AdminCourseApiController.cs:line 122在lambda_方法(闭包、对象、对象[])处 System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.c__DisplayClass10.b__9(对象 实例,对象[]方法参数)位于 System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(对象 实例,对象[]参数),位于 System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext,IDictionary`2参数,CancellationToken cancellationToken)InnerException:


您的文档缺少必需的XML标头。此外,您没有正确转义
字符

尝试在XML文档顶部添加以下内容:

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


并将
替换为
&。(请参见)

&
是XML中的特殊字符。尝试使用
&而不是
&

<SuggestedReadings>
   <Suggestion Text="Customer Centricity" Link="http://wdp.wharton.upenn.edu/book/customer-centricity/?utm_source=Coursera&amp;utm_medium=Web&amp;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&amp;utm_medium=Web&amp;utm_campaign=glbrpower" SuggBy="Barbara Kahn�s" />
</SuggestedReadings>


实际上是错误转义的“与”符号。它只是希望它在形式上是一个转义字符&something;因此,当它到达=时,它抛出错误

[TestMethod]
public void BadXml()
{
    string 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>";

    XmlDocument xdoc = new XmlDocument();
    xml = xml.Replace("&", "&amp;");
    xdoc.LoadXml(xml);

}
[TestMethod]
public-void-BadXml()
{
字符串xml=”“;
XmlDocument xdoc=新的XmlDocument();
xml=xml.Replace(“&”、“&;”;
LoadXml(xml);
}
XML预定义了以下五个实体引用,用于特殊用途 否则将被解释为标记一部分的字符 语言

<->
>   ->  
"   ->  "
“->&apos;
&->&;
可以使用实体和字符引用转义左角度 括号、符号和其他分隔符。您也可以使用数字 字符引用。数字字符引用被展开 当他们被认出的时候。另外,因为 字符引用被视为字符数据,您可以使用 数字字符引用

希望这个链接能帮助你

如何使用Visual C#.NET查找和替换XML文件中的特殊字符

如何在C中转义XML字符串的不同方法#

什么是
CD.SRList
?它实际上是被错误转义的与号。它只是希望它在形式上是一个转义字符&something;因此,当它到达=时,它会抛出错误。@JenishRabadiya thsi持有xml字符串。…@Raghubar:我刚刚测试了它,
XmlDocument
不需要xml头,但根据规范,它应该有它。因此,这次您可以省略它,但请在.its working中编辑它。但是出现了新的问题,我为读取xml而编写的代码不起作用。因此,请发布一些代码来读取这个xml节点值。
[TestMethod]
public void BadXml()
{
    string 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>";

    XmlDocument xdoc = new XmlDocument();
    xml = xml.Replace("&", "&amp;");
    xdoc.LoadXml(xml);

}
<   ->  &lt;
>   ->  &gt;
"   ->  &quot;
'   ->  &apos;
&   ->  &amp;