gpt4 book ai didi

c# - 解析 EntityName 时出错。 Line1,位置 844

转载 作者:IT王子 更新时间:2023-10-29 04:10:16 25 4
gpt4 key购买 nike

我从下面的代码块中得到以下异常。

解析 EntityName 时出错。 Line1,位置 844。

我试图将从表中检索到的数据集解析为数据集。

public DataSet BindMasterData(string xml)
{
DataSet ds = null;
try
{
ds = new DataSet();
TextReader txtReader = new StringReader(xml);
XmlReader reader = new XmlTextReader(txtReader);
ds.ReadXml(reader);
}
catch (Exception ex)
{
return new DataSet();
}
return ds;
}

我已经找到异常的原因了,但是我无法解决。在这种特殊情况下,字符串(从数据库中检索)包含一个特殊字符 (&)。这会导致异常。我该如何解决。在这方面的任何帮助都会很棒。

最佳答案

只需替换它们:

在 XML 元素中无效:

"   "
' '
< &lt;
> &gt;
& &amp;

  public static string UnescapeXMLValue(string xmlString)
{
if (xmlString == null)
throw new ArgumentNullException("xmlString")

return xmlString.Replace("&apos;", "'").Replace("&quot;", "\"").Replace("&gt;", ">").Replace("&lt;", "<").Replace("&amp;", "&");
}

public static string EscapeXMLValue(string xmlString)
{

if (xmlString == null)
throw new ArgumentNullException("xmlString")

return xmlString.Replace( "&","&amp;").Replace("'","&apos;").Replace( "\"", "&quot;").Replace(">","&gt;").Replace( "<","&lt;");
}

关于c# - 解析 EntityName 时出错。 Line1,位置 844,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23541910/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com