linq深入查询多个级别

时间:2016-05-23 19:34:06

标签: c# xml linq select

美好的一天,

如何查询以下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>

2 个答案:

答案 0 :(得分:3)

我认为你想要实现的是:

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,您将获得属于的政策和时间表之间的笛卡尔积。

答案 1 :(得分:1)

我使用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();
        }
    }
}