简体   繁体   中英

How do I show multi-level elements using linq in c#?

Following is my XML input:

<?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 name="1">
      <title>First Title</title>
      <break name="1-1"/>
      <pps>This is Sparta</pps>
      <h1>
        <page num="1"/>First Heading
      </h1>
      <bl>This is another text</bl>
      <fig>
        <img src="images/img_1-1.jpg" alt=""/>
        <fc>This is a caption</fc>
      </fig>
      <p>
        <b>This</b> again is<b> a paragraph</b>
      </p>
    </sec>
    <sec name="2">
      <title>Second Title</title>
      <break name="1-1"/>
      <h1>
        <page num="1"/>Second Heading
      </h1>
      <bl>This is another text</bl>
      <fig>
        <img src="images/img_2-1.jpg" alt=""/>
        <fc>This is a caption</fc>
        <cr>This is a credit</cr>
      </fig>
      <p>This is a paragraph</p>
    </sec>
    <sec name="3">
      <title>First Title</title>
      <break name="3-1"/>
      <h1>
        <page num="1"/>Third Heading
      </h1>
      <bl>This is another text</bl>
      <fig>
        <img src="images/img_3-1.jpg" alt=""/>
        <fc>This is a caption</fc>
      </fig>
      <p>This is a paragraph</p>
    </sec>
    <sec name="4">
      <title>Third Title</title>
      <break name="4-1"/>
      <h1>
        <page num="1"/>Fourth Heading
      </h1>
      <bl>This is another text</bl>
      <p>This is a paragraph</p>
      <fig>
        <img src="images/img_4-1.jpg" alt=""/>
        <fc>This is a caption</fc>
        <cr>This is a credit</cr>
      </fig>
      <break name="5-1"/>
      <h1>
        <page num="1"/>Fifth Heading
      </h1>
      <bl>This is another text</bl>
      <fig>
        <img src="images/img_5-1.jpg" alt=""/>
        <fc>This is a caption</fc>
        <cr>This is a credit</cr>
      </fig>
      <p>This is a paragraph</p>
    </sec>
  </body>
</repub>

I want to show the <break> tags that have duplicate values. I have achieved that and I am using the following method:

var breaknameduplicate = xdoc.Descendants("sec").Descendants("break")
    .GroupBy(n => n.Attribute("name").Value.Trim()).ToList()
    .Where(g => g.Count() > 1)
    .Select(g => new { Name = g.Key, count = g.Count() })
    .ToList();

string s = string.Join(Environment.NewLine, breaknameduplicate.Select(x => "<break name=\"" + x.Name + "\"/> - " + x.count + " times."));

if (!String.IsNullOrEmpty(s))
{
    MessageBox.Show(s);
}

Now, what I want is that I want to show the <sec> as well for the <break> .

<break name="1-1"> - 2 times in sections 1 and 2.

Presently, I am able to show only the number of times a value has been repeated but I want to be more precise with the output.

Please help.

Regards

You can use this code:

  var xDoc = XDocument.Load(@"path to the file");
  var grouped = xDoc.Descendants("sec")
    .GroupBy(e => e.Descendants("break").First().Attribute("name").Value.Trim())
    .Select(g => $"<break name = \"{g.Key}\"> - {g.Count()} time(s) in section(s) {string.Join(",", g.Select(e => e.Attribute("name").Value))}")
    .ToList();

Naive Approach to get the sec s

var breaknameduplicate = xdoc.Descendants("sec").Descendants("break")
    .GroupBy(n => n.Attribute("name").Value.Trim()).ToList()
    .Where(g => g.Count() > 1)
    .Select(g => new { Name = g.Key, count = g.Count(), Sections = g.Select(e => e.Ancestors("sec").FirstOrDefault()).ToList() })
    .ToList();

EDIT: To get the titles:

var breaknameduplicate = xdoc.Descendants("sec").Descendants("break")
    .GroupBy(n => n.Attribute("name").Value.Trim()).ToList()
    .Where(g => g.Count() > 1)
    .Select(g => new { Name = g.Key, count = g.Count(), 
        Sections = g.Select(e => e.Ancestors("sec")
                                 ?.Elements("title")
                                 ?.FirstOrDefault()
                                 ?.Value)
                          .ToList() 
                      })
    .ToList();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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