Csharp/C#教程:获取XML上的嵌套元素(使用Lambda)并设置为List分享


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

我有一个嵌套List的XML,我需要在我的类对象中的所有属性。

类:

public class Process { public Process() { Progresses = new List(); } public string Code{ get; set; } public List Progresses { get; set; } } public class Progress { public string Text { get; set; } public string ProgressDate { get; set; } } 

XML:

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

到现在为止我有这个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标签,而不是一个进程列表! 我最大的问题是,我可以读取XML并设置为类PROCESS,我需要在PROCESS对象上的List中的所有PROGRESS标记。

更新:有了这个Linq,我只获得了Progress嵌套节点的第一个元素。 我怎么能这样做(如果使用Lambda更好)? 非常感谢!!!

正如我在问题评论中提到的,必须将Progress类声明为public

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

[编辑]

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

上面的查询返回IEnumerableProgresses列表。

上述就是C#学习教程:获取XML上的嵌套元素(使用Lambda)并设置为List分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

 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 

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/988494.html

(0)
上一篇 2021年12月23日
下一篇 2021年12月23日

精彩推荐