C#/XML:'System.Xml.XmlDocument'不包含'Descendants'的定义。

4

我刚才从这段代码中收到了一个错误:

private void ShowXMLDatatoRTB() {
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("XMLFile.xml");

var persons = from person in xmlDoc.Descendants("Person")
        select new
            {
                Name = person.Element("Name").Value,
                    City = person.Element("City").Value,
                Age = person.Element("Age").Value,
            };

richTextBox1.Text = "";
foreach (var person in persons)
{
    richTextBox1.Text = richTextBox1.Text + "Name: " + person.Name + "\n";
            richTextBox1.Text = richTextBox1.Text + "City: " + person.City + "\n";
            richTextBox1.Text = richTextBox1.Text + "Age: " + person.Age + "\n\n";
    }

if (richTextBox1.Text == "")
            richTextBox1.Text = "No Results."; }

我有什么遗漏吗?

3个回答

22

您只需要从XmlDocument切换到XDocument。


4

您试图使用LINQ to SQL,但实际上在使用旧的DOM API。您需要添加对System.XMLSystem.Xml.Linq的引用,并使用XDocument而不是XmlDocument


0

使用

XDocument xmlDoc = new XDocument();

而不是

XmlDocument xmlDoc = new XmlDocument(); `

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接