<?xml version="1.0" encoding="utf-8" ?>
<animals>
<animal id="fisrt">
<type>Dog</type>
<name>Han</name>
</animal>
<animal id="second">
<type>Cat</type>
<name>Leia</name>
</animal>
</animals>
C#
using System.Xml.Linq;
string id = "second";
var filter = from ab in element.Elements("animal") where ab.Attribute("id").Equals(id) select ab;
foreach (XElement selector in filter)
{
label1.Content = selector.Element("name").Value;
}
我需要选择是基于父元素的id元素帮助。目标是选择姓名的父母的ID是“第二”,所以我试图得到“Leia”。我遇到的问题是标签没有任何事情发生。
我在做什么错,我该如何解决这个问题。如果有人知道实现我的目标的更好方法,我也会采取不同的方法。
'ab.Attribute(“id”).Value.Equals(id)'是你正在寻找的。 –