简体   繁体   English

获取 XML 上的嵌套元素(使用 Lambda)并设置为 List<Object>

[英]Get nested elements on XML (with Lambda) and set to List<Object>

I have an XML with a List nested and I need all properties inside my class object.我有一个带有嵌套列表的 XML,我需要我的类对象中的所有属性。

Class:班级:

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: 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>

Until now I have this Linq:到现在为止,我有这个 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()
    };

But with this, I have one register to each Progress tag, instead of a List of Progresses!但是有了这个,我对每个 Progress 标签都有一个寄存器,而不是一个进度列表! 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.我最大的问题是,如何读取 XML 并将其设置为类 PROCESS,我需要 PROCESS 对象的 List 内的所有 PROGRESS 标记。

UPDATE: With this Linq, I'm getting just the first element of Progress nested nodes.更新:使用这个 Linq,我只得到 Progress 嵌套节点的第一个元素。 How can I do that (better if using Lambda)?我该怎么做(如果使用 Lambda 会更好)? Thanks a lot!!!非常感谢!!!

As i mentioned in the comment to the question Progress class have to be declared as public .正如我在问题Progress类必须声明为public的评论中提到的。

Another issue is: Num is not a member of Progress class!另一个问题是: Num不是Progress类的成员!

[EDIT] [编辑]

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

Above query returns IEnumerable<Process> with the list of Progresses .上面的查询返回IEnumerable<Process>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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM