【问题标题】:linq query multiple levels deeplinq 查询多个级别的深度
【发布时间】:2016-05-23 19:34:05
【问题描述】:

早安,

我如何查询以下 xml,例如,如果 ResidenceIsSLP 值 == “否”,则返回策略和计划名称。

此外,这种 XML 格式/布局是否正常或太深,因为当我尝试“深度”超过 3 层时似乎卡住了。

我可以创建一个 var 并仅获取策略,然后从那里创建另一个 var,然后获取更深层次的计划,但是有没有办法从单个查询中做到这一点?

我想我需要一个新的选择并将这两个选择结合起来才能返回警察名称和时间表名称。

提前感谢您的帮助。

<Config>
    <Policies>
        <Policy Number="3">
          <PolicyName>AD_EXCHANGE</PolicyName>
          <General>
            <PerformSnapshot>Enabled</PerformSnapshot>
            <DataClassification>Platinum</DataClassification>
          </General>
          <Clients>
            <Client Number="1">
              <ClientHostname>BORHOMBX</ClientHostname>
              <ClientHardware>Windows-x64</ClientHardware>
              <ClientOS>Windows</ClientOS>
              <ClientPriority>0</ClientPriority>
            </Client>
          </Clients>
          <Schedules>
            <Schedule Number="1">
              <ScheduleName>AD_PLATINUM_DAILY_FULL</ScheduleName>
              <ResidenceIsSLP>Yes</ResidenceIsSLP>
            </Schedule>
            <Schedule Number="2">
              <ScheduleName>AD_PLATINUM_MONTHLY_FULL</ScheduleName>
              <ResidenceIsSLP>Yes</ResidenceIsSLP>
            </Schedule>
          </Schedules>
        </Policy>
    </Policies>
</Config>

【问题讨论】:

  • 您是否尝试过任何操作并出现错误?

标签: c# xml linq select


【解决方案1】:

我认为你想要实现的是:

var result= from e in doc.Decendants("Policy")
            from s in e.Decendants("Schedule")
            where s.Element("ResidenceIsSLP").Value=="No"
            select new {
                          PolicyName= (string)e.Element("PolicyName"),
                          ScheduleName= (string)s.Element("ScheduleName")
                       };

使用双精度 from,您将在属于的策略和时间表之间获得笛卡尔积。

【讨论】:

    【解决方案2】:

    我使用 xml linq 解析了您发布的整个 xml

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml;
    using System.Xml.Linq;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            const string FILENAME = @"c:\temp\test.xml";
            static void Main(string[] args)
            {
                XDocument doc = XDocument.Load(FILENAME);
                var results = doc.Descendants("Policy").Select(x => new {
                    number = (int)x.Attribute("Number"),
                    name = (string)x.Element("PolicyName"),
                    performSnapshot = (string)x.Descendants("PerformSnapshot").FirstOrDefault(),
                    dataClassification = (string)x.Descendants("DataClassification").FirstOrDefault(),
                    clients = x.Descendants("Client").Select(y => new {
                        number = (int)y.Attribute("Number"),
                        clientHostname = (string)y.Element("ClientHostname"),
                        clientHardware = (string)y.Element("ClientHardware"),
                        clientOS = (string)y.Element("ClientOS"),
                        clientPriority = (int)y.Element("ClientPriority"),
                    }).ToList(),
                    schedule = x.Descendants("Schedule").Select(y => new {
                        number = (int)y.Attribute("Number"),
                        scheduleName = (string)y.Element("ScheduleName"),
                        residenceIsSLP = (string)y.Element("ResidenceIsSLP")
                    }).ToList()
                }).ToList();
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-02-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-16
      • 2018-05-14
      • 1970-01-01
      • 2017-01-09
      相关资源
      最近更新 更多