使用Linq从复杂的XML中提取数据

时间:2015-12-01 17:14:42

标签: c# xml linq

嗨,我有一个像XML一样的

<Feed>
<Control>
    <UserName>testUser</UserName>
    <Password>testPass</Password>
</Control>
<leeds>
    <leed>
        <leedid>4990935</leedid>
        <Reference>4990935</Reference>
        <AccidentDetails>
            <IncidentDate>2015-08-05</IncidentDate>
            <AccidentDetails>damage</AccidentDetails>
        </AccidentDetails>
        <ClientDetails>
            <ClientsID>4990935</ClientsID>
            <ClientsName>Test Name</ClientsName>
        </ClientDetails>
        <IncidentDetails>
            <IncidentID>557475</IncidentID>
            <IncidentName>Injury</IncidentName>
        </IncidentDetails>
    </leed>
<leed>
.......... More Leeds 
</leed>


现在我正在尝试使用以下Linq提取数据,但它不起作用,请你帮忙。

XDocument xmlDoc = XDocument.Load(path);

        var results = from job in xmlDoc.Root.Elements("Control")
                      let Repairer = job.Parent.Elements("Leeds").FirstOrDefault()

                      select new Job {
                          Username = (string)job.Element("leedid"),
                          Password = (string)job.Element("Reference"),
                          IncidentDetail = (string)job.Element("IncidentDetail"),
                          ClientsName = (string)job.Element("ClientsName"),
                          IncidentName = (string)Repairer.Element("IncidentName")
                      };
                      return result;

我有Model Class

public class Control {
    public string Username { get; set; }

    public string Password { get; set; }

    public Leed leeds {get; set;}
} 

public class Leed {

    public int leedid { get; set; }

    public int Reference { get; set; }

    public string IncidentDate { get; set; }

    public string IncidentDetail { get; set; }

    public string ClientsID { get; set; }

    public string ClientsName { get; set; }

    public string IncidentID { get; set; }

    public string IncidentName { get; set; }
}

我是Linq和XML的新手,One Feed将拥有一个控件和多个leeds。

2 个答案:

答案 0 :(得分:0)

我必须改变很多东西才能让它发挥作用。标签名称必须与xml

的大写/小写字母相同
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 xml =
               "<Feed>" +
                  "<Control>" +
                    "<UserName>testUser</UserName>" +
                    "<Password>testPass</Password>" +
                  "</Control>" +
                  "<leeds>" +
                    "<leed>" +
                      "<leedid>4990935</leedid>" +
                      "<Reference>4990935</Reference>" +
                      "<IncidentDetails>" +
                        "<IncidentDate>2015-08-05</IncidentDate>" +
                        "<AccidentDetails>damage</AccidentDetails>" +
                      "</IncidentDetails>>" +
                        "<ClientDetails>" +
                          "<ClientsID>4990935</ClientsID>" +
                          "<ClientsName>Test Name</ClientsName>" +
                        "</ClientDetails>" +
                        "<IncidentDetails>" +
                          "<IncidentID>557475</IncidentID>" +
                          "<IncidentName>Injury</IncidentName>" +
                        "</IncidentDetails>" +
                      "</leed>" +
                  "</leeds>" +
                "</Feed>";
            XDocument xmlDoc = XDocument.Parse(xml);

            var results = xmlDoc.Descendants("Feed").Select(feed => 
                          feed.Descendants("leed").Select(job => new Job
                          {
                              Username = (string)feed.Elements("Control").FirstOrDefault().Element("UserName"),
                              Password = (string)feed.Elements("Control").FirstOrDefault().Element("Password"),
                              IncidentDetail = string.Join(",", job.Element("IncidentDetails").Elements().Select(x => x.Value)),
                              ClientsName = (string)job.Elements("ClientsName").FirstOrDefault(),
                              IncidentName = (string)job.Descendants("IncidentName").FirstOrDefault()
                          }).ToList()).ToList();
        }
    }
    public class Job
    {
        public string Username { get; set; }

        public string Password { get; set; }

        public int leedid { get; set; }

        public int Reference { get; set; }

        public string IncidentDate { get; set; }

        public string IncidentDetail { get; set; }

        public string ClientsID { get; set; }

        public string ClientsName { get; set; }

        public string IncidentID { get; set; }

        public string IncidentName { get; set; }
    }
}

​

答案 1 :(得分:0)

根据您的XML,您的Control课程不正确。由于您在leed内可能有多个leeds代码,因此您应该在Control类中使用类似的映射,因此请将其更改为接受集合,而不是这样: -

 public List<Leed> leeds { get; set; }

现在,将leeds节点存储在局部变量leeds中,最后再循环多个leeds

这应该给你预期的输出: -

List<Control> result = (from job in xdoc.Root.Elements("Control")
         let leeds = job.Parent.Element("leeds")
         select new Control
            {
               Username = (string)job.Element("UserName"),
               Password = (string)job.Element("Password"),
               leeds = (from leed in leeds.Elements("leed")
                     let AccidentDtls = leed.Element("AccidentDetails")
                     let ClientDetails = leed.Element("ClientDetails")
                     let IncidentDtls = leed.Element("IncidentDetails")
                     select new Leed
                        {
                          leedid = (int)leed.Element("leedid"),
                          Reference = (int)leed.Element("Reference"),
                          IncidentDate = (string)AccidentDtls.Element("IncidentDate"),
                          IncidentDetail = (string)AccidentDtls.Element("IncidentDetail"),
                          ClientsID = (string)ClientDetails.Element("ClientsID"),
                          ClientsName = (string)ClientDetails.Element("ClientsName"),
                          IncidentID = (string)IncidentDtls.Element("IncidentID"),
                          IncidentName = (string)IncidentDtls.Element("IncidentName")
                        }).ToList()
           }).ToList();