C#XML复杂查询到XDocument

杰里德

我有一个来自“选择新的”陈述的复杂集合。我一直试图将其输出到新的“ XDocument”,但无法这样做。感谢您的协助。

源样本:

<GetAssetWarrantyResponse>
  <GetAssetWarrantyResult>
    <Faults />
    <Response>
      <DellAsset>
        <AssetParts nil="true" />
        <CountryLookupCode>11</CountryLookupCode>
        <CustomerNumber>100540040</CustomerNumber>
        <IsDuplicate>false</IsDuplicate>
        <ItemClassCode>UI002</ItemClassCode>
        <LocalChannel>05</LocalChannel>
        <MachineDescription>OPTI 3020,TIGRISSFFFBTX</MachineDescription>
        <OrderNumber>584290163</OrderNumber>
        <ParentServiceTag nil="true" />
        <ServiceTag>1CZTF02</ServiceTag>
        <ShipDate>2014-03-21T00:00:00</ShipDate>
        <Warranties>
          <Warranty>
            <EndDate>2022-03-21T23:59:59</EndDate>
            <EntitlementType>INITIAL</EntitlementType>
            <ItemNumber>422-0052</ItemNumber>
            <ServiceLevelCode>D</ServiceLevelCode>
            <ServiceLevelDescription>DirectLine Service</ServiceLevelDescription>
            <ServiceLevelGroup>11</ServiceLevelGroup>
            <ServiceProvider>DELL</ServiceProvider>
            <StartDate>2014-03-21T00:00:00</StartDate>
          </Warranty>
          <Warranty>
            <EndDate>2017-03-21T23:59:59</EndDate>
            <EntitlementType>INITIAL</EntitlementType>
            <ItemNumber>984-0092</ItemNumber>
            <ServiceLevelCode>KK</ServiceLevelCode>
            <ServiceLevelDescription>Keep Your Hard Drive Service</ServiceLevelDescription>
            <ServiceLevelGroup>11</ServiceLevelGroup>
            <ServiceProvider>DELL</ServiceProvider>
            <StartDate>2014-03-21T00:00:00</StartDate>
          </Warranty>
          <Warranty>
            <EndDate>2017-03-21T23:59:59</EndDate>
            <EntitlementType>EXTENDED</EntitlementType>
            <ItemNumber>939-7358</ItemNumber>
            <ServiceLevelCode>ND</ServiceLevelCode>
            <ServiceLevelDescription>Next Business Day Support</ServiceLevelDescription>
            <ServiceLevelGroup>5</ServiceLevelGroup>
            <ServiceProvider>UNY</ServiceProvider>
            <StartDate>2015-03-22T00:00:00</StartDate>
          </Warranty>
          <Warranty>
            <EndDate>2015-03-21T23:59:59</EndDate>
            <EntitlementType>INITIAL</EntitlementType>
            <ItemNumber>939-6868</ItemNumber>
            <ServiceLevelCode>ND</ServiceLevelCode>
            <ServiceLevelDescription>Next Business Day Support</ServiceLevelDescription>
            <ServiceLevelGroup>5</ServiceLevelGroup>
            <ServiceProvider>UNY</ServiceProvider>
            <StartDate>2014-03-21T00:00:00</StartDate>
          </Warranty>
          <Warranty>
            <EndDate>2022-03-24T23:59:59</EndDate>
            <EntitlementType>INITIAL</EntitlementType>
            <ItemNumber>421-9982</ItemNumber>
            <ServiceLevelCode>D</ServiceLevelCode>
            <ServiceLevelDescription>DirectLine Service</ServiceLevelDescription>
            <ServiceLevelGroup>11</ServiceLevelGroup>
            <ServiceProvider>DELL</ServiceProvider>
            <StartDate>2014-03-21T00:00:00</StartDate>
          </Warranty>
          <Warranty>
            <EndDate>2022-03-24T23:59:59</EndDate>
            <EntitlementType>INITIAL</EntitlementType>
            <ItemNumber>525-0013</ItemNumber>
            <ServiceLevelCode>D</ServiceLevelCode>
            <ServiceLevelDescription>DirectLine Service</ServiceLevelDescription>
            <ServiceLevelGroup>11</ServiceLevelGroup>
            <ServiceProvider>DELL</ServiceProvider>
            <StartDate>2014-03-21T00:00:00</StartDate>
          </Warranty>
          <Warranty>
            <EndDate>2022-03-24T23:59:59</EndDate>
            <EntitlementType>INITIAL</EntitlementType>
            <ItemNumber>422-0008</ItemNumber>
            <ServiceLevelCode>D</ServiceLevelCode>
            <ServiceLevelDescription>DirectLine Service</ServiceLevelDescription>
            <ServiceLevelGroup>11</ServiceLevelGroup>
            <ServiceProvider>DELL</ServiceProvider>
            <StartDate>2014-03-21T00:00:00</StartDate>
          </Warranty>
        </Warranties>
      </DellAsset>
      <DellAsset>
        <AssetParts nil="true" />
        <CountryLookupCode>11</CountryLookupCode>
        <CustomerNumber>100540040</CustomerNumber>
        <IsDuplicate>false</IsDuplicate>
        <ItemClassCode>7M002</ItemClassCode>
        <LocalChannel>05</LocalChannel>
        <MachineDescription>POWEREDGE R720XD, ORCA S PE</MachineDescription>
        <OrderNumber>339791846</OrderNumber>
        <ParentServiceTag nil="true" />
        <ServiceTag>1VF0TW1</ServiceTag>
        <ShipDate>2013-03-20T00:00:00</ShipDate>
        <Warranties>
          <Warranty>
            <EndDate>2016-03-20T23:59:59</EndDate>
            <EntitlementType>INITIAL</EntitlementType>
            <ItemNumber>936-7263</ItemNumber>
            <ServiceLevelCode>SV</ServiceLevelCode>
            <ServiceLevelDescription>Silver Premium Support</ServiceLevelDescription>
            <ServiceLevelGroup>8</ServiceLevelGroup>
            <ServiceProvider>DELL</ServiceProvider>
            <StartDate>2013-03-20T00:00:00</StartDate>
          </Warranty>
          <Warranty>
            <EndDate>2016-03-20T23:59:59</EndDate>
            <EntitlementType>INITIAL</EntitlementType>
            <ItemNumber>983-6402</ItemNumber>
            <ServiceLevelCode>KK</ServiceLevelCode>
            <ServiceLevelDescription>Keep Your Hard Drive Service</ServiceLevelDescription>
            <ServiceLevelGroup>11</ServiceLevelGroup>
            <ServiceProvider>DELL</ServiceProvider>
            <StartDate>2013-03-20T00:00:00</StartDate>
          </Warranty>
          <Warranty>
            <EndDate>2016-03-20T23:59:59</EndDate>
            <EntitlementType>EXTENDED</EntitlementType>
            <ItemNumber>936-7243</ItemNumber>
            <ServiceLevelCode>ND</ServiceLevelCode>
            <ServiceLevelDescription>Next Business Day Support</ServiceLevelDescription>
            <ServiceLevelGroup>5</ServiceLevelGroup>
            <ServiceProvider>UNY</ServiceProvider>
            <StartDate>2014-03-21T00:00:00</StartDate>
          </Warranty>
          <Warranty>
            <EndDate>2014-03-20T23:59:59</EndDate>
            <EntitlementType>INITIAL</EntitlementType>
            <ItemNumber>989-2701</ItemNumber>
            <ServiceLevelCode>ND</ServiceLevelCode>
            <ServiceLevelDescription>Next Business Day Support</ServiceLevelDescription>
            <ServiceLevelGroup>5</ServiceLevelGroup>
            <ServiceProvider>UNY</ServiceProvider>
            <StartDate>2013-03-20T00:00:00</StartDate>
          </Warranty>
        </Warranties>
      </DellAsset>
      </Response>
    </GetAssetWarrantyResult>
</GetAssetWarrantyResponse>

这是我到目前为止的内容:

    var xlQueryTest = (from mainRequest in testing.Descendants("Response")
                  select new
                  {
                      Baseinfo = (from baseInfo in mainRequest.Descendants("DellAsset")
                                  select new
                                  {
                                      MachineName = baseInfo.Element("MachineDescription").Value,
                                      ServiceTag = baseInfo.Element("ServiceTag").Value,
                                      WarrantyStart = baseInfo.Element("ShipDate").Value,
                                      Warranties = (from warranty in baseInfo.Descendants("Warranties")
                                                    select new
                                                    {
                                                        Service = (string)warranty.Element("ServiceLevelDescription").Value,
                                                        Provider = (string)warranty.Element("ServiceProvider").Value,
                                                        StartDate = (string)warranty.Element("StartDate").Value,
                                                        EndDate = (string)warranty.Element("EndDate").Value,
                                                        TypeOfWarranty = (string)warranty.Element("EntitlementType").Value
                                                    }).GroupBy(x => x.Service)
                                  }).AsEnumerable().ToList()
                  });

var newDocument = new XDocument(new XElement("Request",
    xlQueryTest.Select(a => a.Baseinfo).Select( 
        //HOWTO
        //iterate through "BaseInfo" Elements
        //iterate through "Warranties" Collection 

我在获取收藏时遇到困难

我想要达到的结果是:

<Request>
    <BaseInfo>
        <MachineName>a</MachineName>
        <ServiceTag>a12345</ServiceTag>
        <ShipDate>01/01/2000</ShipDate>
            <Warranties>
                <Warranty>
                    <ServiceLevelDescription>dfdlkfj</ServiceLevelDescription>
                    <ServiceProvider>ABC</ServiceProvider>
                    <StartDate>01/01/2001</StartDate>
                    <EndDate>01/05/2007</EndDate>
                </Warranty>
                <Warranty>
                </Warranty>
            </Warranties>
    </BaseInfo>
    <BaseInfo>
        <MachineName>b</MachineName>
        <ServiceTag>b12345</ServiceTag>
        <ShipDate>01/01/2010</ShipDate>
            <Warranties>
                <Warranty>
                    <ServiceLevelDescription>dfdlkfj</ServiceLevelDescription>
                    <ServiceProvider>ABCF</ServiceProvider>
                    <StartDate>01/01/2011</StartDate>
                    <EndDate>01/05/2017</EndDate>
                </Warranty>
                <Warranty>
                </Warranty>
            </Warranties>
    </BaseInfo>
</Request>

更新:我将其汇总以尝试进一步解释我要做什么。

我如何将其转换为“新的XDocument”以获得所需的结果(上述)

foreach(var request in xlQueryTest)
            {
                //<Request>

                foreach(var machine in request.Baseinfo)
                {
                    //<BaseInfo>
                        //<MachineName>machine.MachineName</MachineName>
                        //<ServiceTag>machine.ServiceTag</ServiceTag>
                        //<ShipDate>machine.WarrantyStart</ShipDate>
                        //  <Warranites>
                        foreach(var warranty in machine.Warranties)
                        {
                            //<Warranty>
                                //<ServiceLevelDescription>warranty.Service</ServiceLevelDescription>
                                //<ServiceProvider>warranty.Provier</ServiceProvider>
                                //<StartDate>warranty.StartDate</StartDate>
                                //<EndDate>warranty.EndDate</EndDate>
                            //</Warranty>
                        }
                        //  </Warranties>
                    }
                    //</BaseInfo>
                }
                //</Request>
            }

答:我接受har07的答案,因为它解决了重复的问题。Jdweng的解决方案很优雅,但是不能纠正重复的信息。

var xlQueryTest = (from mainRequest in testing.Descendants("Response")
                               select new
                               {
                                   Baseinfo = (from baseInfo in mainRequest.Descendants("DellAsset")
                                               select new
                                               {
                                                   MachineName = baseInfo.Element("MachineDescription").Value,
                                                   ServiceTag = baseInfo.Element("ServiceTag").Value,
                                                   WarrantyStart = baseInfo.Element("ShipDate").Value,
                                                   Warranties = (from warranty in baseInfo.Descendants("Warranty")
                                                                 select new
                                                                 {
                                                                     Service = (string)warranty.Element("ServiceLevelDescription"),
                                                                     Provider = (string)warranty.Element("ServiceProvider"),
                                                                     StartDate = (string)warranty.Element("StartDate"),
                                                                     EndDate = (string)warranty.Element("EndDate"),
                                                                     TypeOfWarranty = (string)warranty.Element("EntitlementType")
                                                                 }).GroupBy(x => x.Service)
                                               }).AsEnumerable().ToList()
                               });
                        var newDocument =
                            new XDocument(new XElement("Request",
                            from t in xlQueryTest
                            from q in t.Baseinfo
                            select
                            new XElement("BaseInfo",
                                new XElement("MachineDescription", q.MachineName),
                                new XElement("ServiceTag", q.ServiceTag),
                                new XElement("ShipDate", q.WarrantyStart),
                                new XElement("Warranites",
                                from g in q.Warranties
                                select
                                    new XElement("Warranty",
                                    new XElement("ServiceLevelDescription", g.Key),
                                    new XElement("ServiceProvider", g.First().Provider),
                                    new XElement("StartDate", g.First().StartDate),
                                    new XElement("EndDate", g.First().EndDate)
                                )
                            )
                        )
                    ));

感谢大家的帮助。

har07

假设您希望Baseinfo.Warranties仅在输出XML中扩展分组保修,这是一种可能的方式:

var newDocument =
        new XDocument(new XElement("Request",
            from query in xlQueryTest
            from baseInfo in query.Baseinfo
            select
            new XElement("BaseInfo",
                new XElement("MachineDescription", baseInfo.MachineName),
                new XElement("ServiceTag", baseInfo.ServiceTag),
                new XElement("ShipDate", baseInfo.WarrantyStart),
                new XElement("Warranites",
                    from grp in baseInfo.Warranties
                    from warranty in grp
                    select
                    new XElement("Warranty",
                        new XElement("ServiceLevelDescription", warranty.Service),
                        new XElement("ServiceProvider", warranty.Provider),
                        new XElement("StartDate", warranty.StartDate),
                        new XElement("EndDate", warranty.EndDate)
                    )
                )
            )
        ));

dotnetfiddle demo

如果您希望将分组的项目输出为一个元素,例如仅显示当前组的第一项的值,则将<Warranites>元素部件的创建修改为以下内容:

new XElement("Warranites",
    from g in q.Warranties
    select
    new XElement("Warranty",
        new XElement("ServiceLevelDescription", g.Key),
        new XElement("ServiceProvider", g.First().Provider),
        new XElement("StartDate", g.First().StartDate),
        new XElement("EndDate", g.First().EndDate)
    )
)

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

C#XML复杂查询到XDocument

来自分类Dev

C#XML避免使用XDocument进行html编码

来自分类Dev

C#Xml到Linq删除项目

来自分类Dev

C#XML XDocument解析)如何知道具有特定孙代值的特定祖先节点的名称?

来自分类Dev

C#XML。从XML获取数据到对象,在UI中更新,保存回XML

来自分类Dev

C#XML差异算法

来自分类Dev

C#XML到达节点

来自分类Dev

读取C#Xml文件

来自分类Dev

C#XML差异算法

来自分类Dev

复杂的SQL Server查询到LINQ to SQL查询C#

来自分类Dev

如何使用XDocument从c#中的复杂类型xml解析文本?

来自分类Dev

Linq to SQL的C#XML文档

来自分类Dev

C#XML注释中的尖括号

来自分类Dev

C#XML解析双重标记列表

来自分类Dev

C#XML搜索节点值

来自分类Dev

浮点值的C#XML问题

来自分类Dev

用 c# Xml 解析复杂的 XML 到 linq

来自分类Dev

实体框架复杂查询到嵌套的C#对象

来自分类Dev

C#XDocument XML格式

来自分类Dev

插入/替换xml到Xdocument的指定节点

来自分类Dev

XML节点中的C#XML元素计数

来自分类Dev

复杂的 LINQ to XML 查询帮助

来自分类Dev

复杂的XML解析到标签

来自分类Dev

查询Xdocument

来自分类Dev

C#XML序列化元素名称空间

来自分类Dev

c#XML序列化:名称空间声明的顺序

来自分类Dev

C#XML序列化必填字段

来自分类Dev

具有XmlAttribute标签的C#XML序列化

来自分类Dev

C#Xml分组可实现更好的可视化