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

abr*_*pin 4 c# xml definition

我刚刚从这段代码收到错误:

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."; }
Run Code Online (Sandbox Code Playgroud)

我错过了什么吗?

Jay*_*ard 21

您只需要从XmlDocument切换到XDocument.