我纠正了你的第一次尝试(LINQ)。它首先返回
<PName type="Patient">
从讨论中的xml。
IEnumerable<XElement> tests =
from el in root.Element("PList").Elements("PName")
let c = el.Descendants("classification")
where c.Where(x => x.Attribute("classification").Value == "paymenttype"
&& x.Attribute("category").Value == "Wallet").Any()
select el;
然后你可以迭代
tests
foreach (var el in tests)
{
Console.WriteLine(
el.Element("properties")
.Element("PName")
.Attribute("title")
.Value);
}
我还更正了您的第二次尝试(XPath)。它将返回的标题值
<PName title="Joe Beom" PId="1234">
.
var query = @"//PName[Details/classification[@classification='paymenttype' and @category='Wallet']]/@title";
foreach (XmlNode n in docs.SelectNodes(query))
{
Console.WriteLine(n.Value);
}