且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何基于另一个属性的值选择XML中的属性?

更新时间:2023-01-08 17:13:19

类似的方法将起作用:

string xml = "YourXml";
XElement doc = XElement.Parse(xml);

var Result = from a in doc.Descendants("yweather:forecast")
             where a.Attribute("day").Value == "Mon"
             select a.Attribute("text").Value;

或lambda语法:

var Result = doc.Descendants("yweather:forecast")
                .Where(x=> x.Attribute("day").Value == "Mon")
                .Select(x=> x.Attribute("text").Value);

您还可以参考此 SO帖子