Click here to Skip to main content
15,896,912 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Hello!

My XML is as follows (format):
XML
<!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:

C#
// 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);
}
Posted
Updated 8-Feb-19 20:19pm

1 solution

Got the solution:

C#
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();
 
Share this answer
 
Comments
Maciej Los 9-Feb-19 15:34pm    
5ed!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900