如何在XML C#中的每个父元素中读取重复值? [英] How do I read duplicate values in each parent element in XML C#?

查看:93
本文介绍了如何在XML C#中的每个父元素中读取重复值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好!



我的XML如下(格式):

 <  !DOCTYPE     repub     SYSTEM    C:\\\\\\\\\\\\\\ n  >  
<? xml-stylesheet href = C:\\\\\\\\\\\\ type = text / xsl >
< repub >
< head >
< title > 默认标题< / title >
< / head >
< body < span class =code-keyword>>

< sec >
< title > TITLE < / title >
< break name = 1-1 >
< p > 文本1 < / p >
< 标题 > < page num = 1 / > 标题 < / heading >
< 子标题 > 小标题< / subheading >
< fig > < img src = images / img_1-1.jpg alt = / > < fc > 图片标题< / fc > < cr > 图片积分< / cr > < / fig >
< fig > < img src = images / img_1-2.jpg alt = / > < fc > 图片标题< / fc > < / fig >
< fig > < img src = images / img_1-3.jpg alt = / > < ; fc > 图片标题< / fc > < cr > 图片积分< / cr > < < span class =code-leadattribute> / fig >
< / break >

< ; break name < span class =code-keyword> = 2-1 >
< p > 文字2 < / p >
< 标题 > < page num = 2 / > 标题2 < / heading >
< 子标题 > 小标题2 < / subheading >
< fig > < img src = images / img_2-1.jpg alt = / > < fc > 图片标题< / fc > < cr > 图片积分2 < / cr > < < span class =code-leadattribute> / fig >
< fig > < img src = images / img_2-2.jpg alt = / > < / fig >
< fig > < img src = < span class =code-keyword> images / img_2-3.jpg alt = / > < fc > 图片标题< / fc > < cr > Image Credit < span class =code-keyword>< / cr > < / fig >
< span class =code-keyword>< / break >
< / sec >
< / body >
< / repub > ;





我希望将中断名称值作为输出,因为该中断具有重复的< cr>价值。



我没有得到。



请帮忙。



问候

Aman



我的尝试:


  //   xdoc是包含具有上述结构的主XML的XDocument  
var creditvalueduplicate = xdoc.Descendants( break)。SelectMany(br = > br.Descendants( cr)。选择(p = > < span class =code-keyword> new
{
crd = br.Value.ToString(),
}))
.GroupBy(x = < span class =code-keyword>> new {x.crd})
.Select(grp = > new
{
id = grp.Key,
count = grp.Count()
})。ToList()。Where(x = > x.count > 1
。 ToList();

string s = string .Join( |,creditvalueduplicate.Select(x = > < cr> + x.id + - + x.count + 次。 ));

if (!String.IsNullOrEmpty(s))
{
AddMsg(s);
}

解决方案

解决方案:



< pre lang =c#> var creditduplicate = xdoc.Descendants( break)。SelectMany(br = > br.Descendants( cr)。选择(p = > new
{
brk = br.Attribute( name) .Value.ToString(),
crd = p.Value.ToString()
}))
.GroupBy(x = > new {x.brk,x.crd})
。选择(grp = > new
{
id = grp.Key,
count = grp.Count()
})。 ToList()
.Where(x = > x.count > 1
.ToList();


Hello!

My XML is as follows (format):

<!DOCTYPE repub SYSTEM "C:\repub\Repub_V1.dtd">
<?xml-stylesheet href="C:\repub\repub.xsl" type="text/xsl"?>
<repub>
<head>
<title>Default Title</title>
</head>
<body>
<sec>
<title>TITLE</title>
<break name="1-1">
<p>Text 1</p>
<heading><page num ="1"/>Heading</heading>
<subheading>Subheading</subheading>
<fig><img src="images/img_1-1.jpg" alt=""/><fc>Image Caption</fc><cr>Image Credit</cr></fig>
<fig><img src="images/img_1-2.jpg" alt=""/><fc>Image Caption</fc></fig>
<fig><img src="images/img_1-3.jpg" alt=""/><fc>Image Caption</fc><cr>Image Credit</cr></fig>
</break>

<break name="2-1">
<p>Text 2</p>
<heading><page num ="2"/>Heading 2</heading>
<subheading>Subheading 2</subheading>
<fig><img src="images/img_2-1.jpg" alt=""/><fc>Image Caption</fc><cr>Image Credit 2</cr></fig>
<fig><img src="images/img_2-2.jpg" alt=""/></fig>
<fig><img src="images/img_2-3.jpg" alt=""/><fc>Image Caption</fc><cr>Image Credit</cr></fig>
</break>
</sec>
</body>
</repub>



I want to get the break name value as the output since that break has a duplicate <cr> value.

I am not getting that.

Please help.

Regards
Aman

What I have tried:

// xdoc is the XDocument that contains the main XML with the mentioned structure
var creditvalueduplicate = xdoc.Descendants("break").SelectMany(br => br.Descendants("cr").Select(p => new
{
	crd = br.Value.ToString(),
}))
.GroupBy(x => new { x.crd })
.Select(grp => new
{
	id = grp.Key,
	count = grp.Count()
}).ToList().Where(x => x.count > 1)
.ToList();

string s = string.Join("|", creditvalueduplicate.Select(x => "<cr>" + x.id +" - " + x.count + " times."));

if (!String.IsNullOrEmpty(s))
{
	AddMsg(s);
}

解决方案

Got the solution:

var creditduplicate = xdoc.Descendants("break").SelectMany(br => br.Descendants("cr").Select(p => new
{
	brk = br.Attribute("name").Value.ToString(),
	crd = p.Value.ToString()
}))
.GroupBy(x => new { x.brk, x.crd })
.Select(grp => new
{
	id = grp.Key,
	count = grp.Count()
}).ToList()
.Where(x => x.count > 1)
.ToList();


这篇关于如何在XML C#中的每个父元素中读取重复值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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