有人可以帮助我使用正确的LINQ语法从这个XML中提取状态吗?
理想情况下,我希望打印出以下内容:
状态:打开= 1
状态:待定= 2
状态:忽略= 3
状态:已关闭= 4
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<scope name="com">
<scope name="company">
<scope name="app">
<scope name="app_monitor">
<scope name="statuses">
<entry name="Open">1</entry>
<entry name="Pending">2</entry>
<entry name="Ignored">3</entry>
<entry name="Closed">4</entry>
</scope>
<scope name="urgencies">
<entry name="Critical">1</entry>
<entry name="Alarm">2</entry>
<entry name="Info">3</entry>
</scope>
</scope>
</scope>
</scope>
</scope>
</configuration>
我尝试了不同的变化,但这里有多远:
XDocument Xdocument = new XDocument();
var doc = XDocument.Load(@"c:\temp\app_sett.xml");
var returnedvalues = from app_sett in doc.Descendants("scope")
where app_sett.Attribute("name").Value == "statuses"
select new
{
blah = app_sett.Attribute("name").Value,
};
答案 0 :(得分:2)
听起来你需要获得相关范围元素的子元素。例如:
var query = doc.Descendants("scope")
.Where(x => (string) x.Attribute("name") == "statuses")
.Elements("entry")
.Select(entry => new { Name = (string) entry.Attribute("name"),
Value = (int) entry });