【问题标题】:How do I show multi-level elements using linq in c#?如何在 C# 中使用 linq 显示多级元素?
【发布时间】:2019-04-08 07:39:17
【问题描述】:

以下是我的 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 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>

我想显示具有重复值的&lt;break&gt; 标记。我已经做到了,我正在使用以下方法:

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

现在,我想要显示&lt;sec&gt; 以及&lt;break&gt;

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

目前,我只能显示一个值被重复的次数,但我希望输出更精确。

请帮忙。

问候

【问题讨论】:

  • 你的意思是这样的&lt;break name="1-1"&gt; - 2 times in sections &lt;sec&gt; 1 &lt;/sec&gt; and &lt;sec&gt; 2 &lt;/sec&gt;
  • @Aarif - 是的,这会奏效。虽然,我只接受价值观。

标签: c# .net linq-to-xml


【解决方案1】:

您可以使用此代码:

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

【讨论】:

    【解决方案2】:

    获取secs 的简单方法

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

    编辑:获取标题:

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

    【讨论】:

    • 有效,但它会获取 &lt;sec&gt; 标记中的所有内容。我只想要&lt;sec&gt;中的&lt;title&gt;
    • 完美运行。非常感谢您的帮助:)
    猜你喜欢
    • 2018-10-10
    • 1970-01-01
    • 2021-07-22
    • 1970-01-01
    • 2021-07-13
    • 2019-03-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多