【问题标题】:C# XML LINQ Syntax on Attributes属性上的 C# XML LINQ 语法
【发布时间】:2013-06-26 23:30:21
【问题描述】:

有人可以帮助我使用正确的 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,
                };  

【问题讨论】:

    标签: xml linq


    【解决方案1】:

    听起来您需要获取相关范围元素的子元素。例如:

    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 });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多