本文介绍了获取XML(使用Lambda)上的嵌套元素,并将其设置为List< Object>.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个嵌套有列表的XML,并且我的类对象中需要所有属性.

I have an XML with a List nested and I need all properties inside my class object.

班级:

public class Process
{
    public Process()
    {
        Progresses = new List<Progress>();
    }

    public string Code{ get; set; }
    public List<Progress> Progresses { get; set; }
}
public class Progress
{
    public string Text { get; set; }
    public string ProgressDate { get; set; }
}

XML:

 <PROCESSES>
      <Process>
        <Number>8754639647985</Number>
        <Progress>
            <Date>09/11/2013</Date>
            <Text>Lorem lalal asdf</Text>
            <Date>10/11/2015</Date>
            <Text>Lorem lal</Text>
        </Progress>
        <Progress>
            <Date>09/12/2016</Date>
            <Text>Lorem aqwq</Text>
            <Date>10/11/2017</Date>
            <Text>Lorem qw</Text>
         </Progress>
      </Process>
      <Process>
        <Number>1121321321321321</Number>
        <Progress>
            <Date>09/11/2013</Date>
            <Text>Lorem lalal asdf</Text>
            <Date>10/11/2015</Date>
            <Text>Lorem lal</Text>
        </Progress>
        <Progress>
            <Date>09/12/2016</Date>
            <Text>Lorem aqwq</Text>
            <Date>10/11/2017</Date>
            <Text>Lorem qw</Text>
        </Progress>
      </Process>
 </PROCESSES>

直到现在我有了这个Linq:

Until now I have this Linq:

var _procs =
    from proc in xml.Root.Elements("Process")
    select new Process()
    {
        Code = (string)proc.Element("Number"),
        Progresses = proc.Elements("Progress")
                           .Select(c => new Progress
                           {
                              Text = (string)c.Element("Text"),
                              ProgressDate = (string)c.Element("Date")
                           }).ToList()
    };

但是,有了这个,我为每个Progress标签分配了一个寄存器,而不是Progress列表!我最大的问题是,如何读取XML并将其设置为Class PROCESS,我需要PROCESS对象的List中的所有PROGRESS标记.

But with this, I have one register to each Progress tag, instead of a List of Progresses! My biggest question is, how can I read the XML and set as the Class PROCESS, I need all the PROGRESS tags inside the List on my PROCESS object.

更新: 使用此Linq,我仅获得Progress嵌套节点的第一个元素.我该怎么做(最好是使用Lambda)?非常感谢!!!

UPDATE: With this Linq, I'm getting just the first element of Progress nested nodes.How can I do that (better if using Lambda)?Thanks a lot!!!

推荐答案

正如我在对问题Progress类的注释中所提到的,必须声明为public.

As i mentioned in the comment to the question Progress class have to be declared as public.

另一个问题是:Num不是Progress类的成员!

Another issue is: Num is not a member of Progress class!

var query = xdoc.Descendants("Process")
.Select(x=> new Process
    {
        Code = x.Element("Number").Value,
        Progresses = x.Descendants("Progress")
            .SelectMany((ele, j)=>  ele.Elements("Date").Select((a, i)=>new{Date = a.Value, Index = i+(j*10)}))
            .Join(x.Descendants("Progress").SelectMany((ele, j)=> ele.Elements("Text").Select((a, i)=>new{Text = a.Value, Index = i+(j*10)})),
                    dat => dat.Index,
                    tex => tex.Index,
                    (dat, tex) => new {D = dat, T = tex})
            .Select(jdata=> new Progress
                {
                    //Index = jdata.D.Index,
                    ProgressDate = jdata.D.Date,
                    Text = jdata.T.Text
                }).ToList<Progress>()
    });

上面的查询返回IEnumerable<Process>Progresses的列表.

Above query returns IEnumerable<Process> with the list of Progresses.

Code                Progresses
8754639647985       Lorem lalal asdf    09/11/2013
                    Lorem lal           10/11/2015
                    Lorem aqwq          09/12/2016
                    Lorem qw            10/11/2017
1121321321321321    Lorem lalal asdf    09/11/2013
                    Lorem lal           10/11/2015
                    Lorem aqwq          09/12/2016
                    Lorem qw            10/11/2017

这篇关于获取XML(使用Lambda)上的嵌套元素,并将其设置为List&lt; Object&gt;.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 03:49