在单个LINQ中透视数据到XML

本文关键字:数据 XML 透视 单个 LINQ | 更新日期: 2023-09-27 18:09:40

这应该很容易。我想获得数据的一些属性,但它不是一种形式,使我很容易在一个单一的语句。如何将其更改为单个XML to LINQ语句?下面是我现在的做法

   XDocument xdoc = XDocument.Parse(result.Result.ToString());
        string id = (from c in xdoc.Descendants("Fields").Elements("Field")
                 where (string)c.Attribute("FieldId") == "1"
                 select new
                 {
                     Id = c.Attribute("Value").Value
                 }).First().Id;
        string firstName = (from c in xdoc.Descendants("Fields").Elements("Field")
                     where (string)c.Attribute("FieldId") == "2"
                     select new
                     {
                         firstName = c.Attribute("Value").Value
                     }).First().firstName;

        <Fields>
          <Field FieldId = "1" Value="1908551075" FieldTitle="Ref Id" FieldType="Text" />
          <Field FieldId = "2" Value="Mary" FieldTitle="First Name" FieldType="Text" />
          <Field FieldId = "3" Value="Crippen" FieldTitle="Last Name" FieldType="Text" />

在单个LINQ中透视数据到XML

我会将其转换为Dictionary

var dict = XDocument.Parse(xmlstring)
            .Descendants("Field")
            .ToDictionary(f => f.Attribute("FieldTitle").Value, 
                          f => f.Attribute("Value").Value);
Console.WriteLine(dict["Ref Id"] + " " +  dict["Last Name"]);

编辑

对于多个字段

var fields  = XDocument.Parse(xmlstring)
             .Descendants("Fields")
             .Select(flds => flds.Descendants("Field")
                             .ToDictionary(f => f.Attribute("FieldTitle").Value, 
                                           f => f.Attribute("Value").Value))
             .ToList();
foreach (var field in fields)
{
    Console.WriteLine(field["Ref Id"] + " " + field["Last Name"]);
}

试试这个

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string input =
             "<Root>" +
             "<Fields>" +
              "<Field FieldId = '"1'" Value='"1908551075'" FieldTitle='"Ref Id'" FieldType='"Text'" />" +
              "<Field FieldId = '"2'" Value='"Mary'" FieldTitle='"First Name'" FieldType='"Text'" />" +
              "<Field FieldId = '"3'" Value='"Crippen'" FieldTitle='"Last Name'" FieldType='"Text'" />" +
              "</Fields>" +
              "</Root>";
            XDocument xDoc = XDocument.Parse(input);
            var results = xDoc.Descendants("Fields").Select(x => new {
                ID = x.Descendants("Field").Where(y => y.Attribute("FieldTitle").Value == "Ref Id").Select(z => z.Attribute("Value").Value).FirstOrDefault(),
                firstName = x.Descendants("Field").Where(y => y.Attribute("FieldTitle").Value == "First Name").Select(z => z.Attribute("Value").Value).FirstOrDefault(),
                lastName = x.Descendants("Field").Where(y => y.Attribute("FieldTitle").Value == "Last Name").Select(z => z.Attribute("Value").Value).FirstOrDefault(),
            }).ToList();

        }
    }
}
​