C#/XML:&x27;Xml.XmlDocument';不包含';后代';
我刚才收到一个来自以下代码的错误:C#/XML:&x27;Xml.XmlDocument';不包含';后代';,c#,xml,definition,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(
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."; }
我遗漏了什么吗?您只需要从XmlDocument切换到XDocument。您正在尝试使用,但实际上使用的是老式的domapi。您需要添加对
System.XML
和System.XML.Linq
的引用,并使用XDocument
,而不是XmlDocument
XDocument xmlDoc = new XDocument();
intead of
XmlDocument xmlDoc = new XmlDocument(); `