解析实体名时发生错误。第1行 [英] an error occurred while parsing entityname. line 1

查看:99
本文介绍了解析实体名时发生错误。第1行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello All,



我在解析XML字符串时遇到异常。



这是异常 解析entityname;时发生的错误。第1行 和我使用的XML如下;

Hello All,

I have got an exception while parsing an XML string.

Here is the exception an error occurred while parsing entityname;. line 1 and the XML I have used is as follows ;

<Details CompanyName="Outlook & Outlook Express" ></Details>

代码如下:

StringReader strRdr = new StringReader(textBox1.Text);
XmlDocument xmlDoc = new XmlDocument();
try
{
    xmlDoc.Load(strRdr);
    XmlNode node = xmlDoc.DocumentElement.SelectSingleNode(@"/Details");
}
catch (Exception Ex)
{
    System.Windows.Forms.MessageBox.Show(Ex.Message.ToString(), "XML Parsing");
}





如果有人可以帮我解决这个问题,那就太棒了。



问候

Sebastian



If someone can help me to solve this issue, that would be great.

Regards
Sebastian

推荐答案

这是因为&在公司名称。在XMLDocument中加载字符串时,它会抛出错误。



您需要在加载之前对其进行编码。以下应该做:

It is because of & in the company name. When the string is being loaded in the XMLDocument it throws the error.

You need to encode it before loading it up. Following should do:
StringReader strRdr = new StringReader("<Details CompanyName=\"Outlook &amp; Outlook Express\" ></Details>");



如果需要,可在需要时将其解码。


If needed, decode it back when needed.


这篇关于解析实体名时发生错误。第1行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆