简体   繁体   English

解析 EntityName 时出错。 第 1 行,位置 844

[英]An error occurred while parsing EntityName. Line1, position 844

I have got the following exception from the below code block.我从下面的代码块中得到了以下异常。

An error occurred while parsing EntityName.解析 EntityName 时出错。 Line1, position 844.第 1 行,位置 844。

I was trying to parse s set of data retrieved from table to a data set.我试图将从表中检索到的数据集解析为数据集。

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;
        }

I have figured out the reason for the exception, but I couldn't solve it.我已经找出了异常的原因,但我无法解决它。 In this particular situation, the string(which is retrieved from DB) contains a special character (&).在这种特殊情况下,字符串(从 DB 检索)包含一个特殊字符 (&)。 That causes exception.这会导致异常。 How I can solve it.我该如何解决。 Any help on this would be great.对此的任何帮助都会很棒。

Just replace them:只需更换它们:

Not valid in XML elements:在 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("'","&apos;").Replace( "\"", "&quot;").Replace(">","&gt;").Replace( "<","&lt;").Replace( "&","&amp;");
  }

This has already been answered, but found a nicer way of achieving the same outcome by doing this in .NET 4.5 by using the Escape method as below:这已经得到了回答,但通过使用 Escape 方法在 .NET 4.5 中找到了一种更好的方法来实现相同的结果,如下所示:

var xmlWithEscapedCharacters = SecurityElement.Escape(xmlWithoutEscapedCharacters);

and then just plug that string into the XML that is being generated.然后将该字符串插入正在生成的 XML 中。

Link: MSDN - SecurityElement.Escape Method链接: MSDN - SecurityElement.Escape 方法

If your XML is being constructed with string concatenation then you'll be wanting to escape it.如果您的 XML 是用字符串连接构造的,那么您将想要对其进行转义。 & should become &amp; &应该变成&amp; while < and > should become &lt;<>应该变成&lt; and &gt;&gt; respectively.分别。

There may be others too.可能还有其他人。 Of course you should really use a proper XML encoder rather than trying to do it yourself for many reasons.当然,您应该真正使用合适的 XML 编码器,而不是出于多种原因尝试自己进行编码。

As the data comes from some data base, the problem can be hot-fixed in the context you described, but I would strongly recommend a different approach.由于数据来自某个数据库,因此可以在您描述的上下文中热修复问题,但我强烈建议采用不同的方法。 The ampersand symbol should not have occured in the xml in the first place, as discussed here .正如此处所讨论的,与符号一开始就不应该出现在 xml 中。 Please clarify how the xml is generated and address the problem there.请阐明 xml 是如何生成的并解决那里的问题。

You can use: SecurityElement.Escape(XML string)您可以使用: SecurityElement.Escape(XML string)

Reference link: https://msdn.microsoft.com/en-us/library/system.security.securityelement.escape(v=vs.110).aspx参考链接: https : //msdn.microsoft.com/en-us/library/system.security.securityelement.escape(v=vs.110).aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM