如何对重复的XML元素进行分组并保存其值? [英] How do I group duplicate XML elements and save its values?

查看:118
本文介绍了如何对重复的XML元素进行分组并保存其值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好!



这是我的XML:



Hello!

This is my XML:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE repub SYSTEM "C:\repub\Repub_V1.dtd">
<?xml-stylesheet href="C:\repub\repub.xsl" type="text/xsl"?>
<repub>
<head>
<title>xxx</title>
</head>
<body>
<sec>
<title>First Title</title>
<break name="1-1">
<heading><page num="1"/>First Heading</heading>
<bl>This is another text</bl>
<p>This is a paragraph</p>
</break>
</sec>
<sec>
<title>Second Title</title>
<break name="2-1">
<heading><page num="1"/>Second Heading</heading>
<bl>This is another text</bl>
<p>This is a paragraph</p>
</break>
</sec>
<sec>
<title>First Title</title>
<break name="3-1">
<heading><page num="1"/>Third Heading</heading>
<bl>This is another text</bl>
<p>This is a paragraph</p>
</break>
</sec>
<sec>
<title>Third Title</title>
<break name="4-1">
<heading><page num="1"/>Fourth Heading</heading>
<bl>This is another text</bl>
<p>This is a paragraph</p>
</break>
<break name="5-1">
<heading><page num="1"/>Fifth Heading</heading>
<bl>This is another text</bl>
<p>This is a paragraph</p>
</break>
</sec>
</body>
</repub>





我想得到< break>的列表每个部分的后代。现在,具有重复名称的部分将合并。我无法获得正确的列表并获得以下输出:





I want to get the list of the <break> descendants for each section. Now, the section with duplicate names will get merged. I am unable to get the proper list and getting the following output:

Quote:

{id = {{ttl = NEWS}},count = 9,vl = {System.Collections.Generic.List<<> f__AnonymousType0< string,System.Collections.Generic.IEnumerable< system.xml.linq。 xelement>>>}}

{ id = {{ ttl = NEWS }}, count = 9, vl = {System.Collections.Generic.List<<>f__AnonymousType0<string, System.Collections.Generic.IEnumerable<system.xml.linq.xelement>>>} }





在调试时,当我点击数值时,我得到以下内容:





While debugging, when I click on the values, I get the following:

Quote:

count = 9

id = {ttl =NEWS }

vl = Count = 9

count=9
id={ ttl = "NEWS" }
vl= Count = 9





现在,当我点击vl时,它会显示9个值,如下所示:





Now, when I click on vl, it shows me 9 values as follows:

Quote:

{ttl =NEWS,val = {System。 Xml.Linq.XContainer。< getdescendants> d__39}}

{ ttl = "NEWS", val = {System.Xml.Linq.XContainer.<getdescendants>d__39} }





现在,当我点击val时,我会逐行获取值。我想在第一次点击中直接显示这9个值,在vl中。



请帮忙。



问候

Aman



我尝试过:





Now, when I click on val, I get the values line by line. I want these 9 values to be shown directly in the first click, in "vl".

Please help.

Regards
Aman

What I have tried:

var anothersectionslist = xdoc.Descendants("sec").SelectMany(br => br.Descendants("break").Select(p => new
{
   ttl = br.Element("title").Value.ToString(), 
   val = br.Descendants()
}))
.GroupBy(x => new { x.ttl }).ToList()
.Select(grp => new
{
   id = grp.Key,
   count = grp.Count(),
   vl = grp.ToList()
}).ToList()
.ToList();

推荐答案

由于许多不必要的调用,我会以这种方式简化您的查询 ToList ()



I'd simplify your query this way, due to many unnecessary calls ToList():

var anothersectionslist = xdoc.Descendants("sec")
    .GroupBy(x => x.Element("title").Value)
    .Select(grp => new
    {
       id = grp.Key,
       count = grp.Count(),
       vl = grp.SelectMany(x=>x.Descendants("break"))
    })
    .ToList();


这篇关于如何对重复的XML元素进行分组并保存其值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆