将数据转换为单个LINQ to XML

时间:2015-08-24 17:50:58

标签: c# xml linq linq-to-xml

这应该很容易。我想得到一些数据的属性,但它不是一种形式,使我很容易进入一个单一的声明。我如何将其更改为单个XML到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" />

2 个答案:

答案 0 :(得分:4)

我会把它转换成字典

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

答案 1 :(得分:1)

试试这个

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


        }
    }
}
​