我有这个 xml 数据
<Categories>
<cat name="Appliances"></cat>
<cat name="Arts, Crafts & Sewing"></cat>
<cat name="Automotive"></cat>
<cat name="Baby"></cat>
</Categories>
我使用此代码将数据读入组合框:
XmlDocument xDoc = new XmlDocument();
xDoc.Load("cat.xml");
XmlNodeList cats = xDoc.GetElementsByTagName("cat");
for (int i = 0; i < cats.Count; i++)
{
comboBox1.Items.Add(cats[i].Attributes["name"].InnerText);
}
但是在 xDoc.Load("cat.xml");
行我收到错误:
An unhandled exception of type 'System.Xml.XmlException' occurred in System.Xml.dll An error occurred while parsing EntityName. Line 3, position 30.
这是什么意思?
最佳答案
您的 xml 包含一个 & 符号,使其无效,请在解析之前尝试此操作
var xmlContent = File.ReadAllText("cat.xml");
XmlDocument xDoc = new XmlDocument();
xDoc.LoadXml(xmlContent.Replace("&", "&"));
关于c# - 当我尝试加载 xml 文件时,为什么出现解析 EntityName 时出错?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44102178/