繁体   English   中英

如何使用linq检查元素在XML C#中是否具有正确的子元素?

[英]How do I use linq to check if an element has the correct child in XML C#?

我的XML中有一些值,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<repub>
  <head>
    <title>Title</title>
  </head>
  <body>
    <sec name="sec_1">
      <title>First Title</title>
      <break name="article_1-1">
        <h1>
          <page num="1"/>
          <b>First Heading</b>
        </h1>
        <h2>First Subheading</h2>
        <fig>
          <img src="images/img_1-1.jpg" width="826" height="657" alt=""/>
          <fc>
            <i>Image Caption</i>
          </fc>
        </fig>
        <fig>
          <cr>This is a credit</cr>
        </fig>
      </break>
      </sec>
    <sec name="sec_2">
      <title>Second Title</title>
      <break name="article_2-1">
        <h1>
          <page num="2"/>
          <b>Second Heading</b>
        </h1>
        <h2>Second Subheading</h2>
        <fig>
          <cr>This is another credit</cr>
        </fig>
      </break>
      </sec>
  </body>
</repub>

我要显示的是包含<fig><break name> ,而<fig>不包含<img src>

我正在执行以下操作:

var breaklist = xdoc.Descendants("break").ToList();

foreach (var eachbreak in breaklist)
{
    var emptyfig = eachbreak.Descendants("fig").ToList();
    foreach (var eachfig in emptyfig)
    {
        if (eachfig.Descendants("img").Count() == 0
         && eachfig.Descendants("cr").Count() > 0)
        {
            AddMsg("Empty <fig> tag with only credit in: " + eachbreak.Attribute("name").Value);
        }
    }
}

返回:

Empty <fig> tag with only credit in: article_1-1
Empty <fig> tag with only credit in: article_2-1

它有效,但是我想使用linq方法。

请帮忙。

问候

1)使用传统方法的Xml To Linq查询:

var result = (from br in xdoc.Descendants("break")
              from fig in br.Descendants("fig")
              where fig.Descendants("img").Count() == 0 && fig.Descendants("cr").Count() > 0
              select new
              {
                  Message = "Empty <fig> tag with only credit in: " + br.Attribute("name").Value
              }).ToList();

2)使用Lambda方法的Xml To Linq查询:

var result = xdoc.Descendants("break")
.SelectMany(br => br.Descendants("fig")
                    .Where(fig => fig.Descendants("img").Count() == 0 && fig.Descendants("cr").Count() > 0)
                    .Select(x => new
                    {
                         Message = "Empty <fig> tag with only credit in: " + br.Attribute("name").Value
                    })
                    ).ToList();

从以上两个result将结果打印到控制台

foreach (var message in result)
{
     Console.WriteLine(message.Message);
}

您可以将其与AddMsg一起使用,例如,

foreach (var message in result)
{
    AddMsg(message.Message);
} 

输出:

在此处输入图片说明

像这样:

string xml = @"<?xml version=""1.0"" encoding=""ISO-8859-1"" standalone=""yes""?>
                <repub>
            <head>
            <title>Title</title>
            </head>
            <body>
            <sec name = ""sec_1"">
                <title> First Title </title>
                <break name = ""article_1-1"">
                <h1><page num = ""1"" /><b> First Heading </b></h1>
                <h2> First Subheading </h2>
                <fig>
                    <img src = ""images/img_1-1.jpg"" width = ""826"" height = ""657"" alt = """" />
                    <fc><i> Image Caption </i></fc>
                </fig>
                <fig><cr> This is a credit </cr></fig>
                </break>
            </sec>
                         </body>
            </repub>";

XDocument xdoc = XDocument.Parse(xml);

var figs = xdoc
            .Descendants("fig").Where(x => (x.Element("img") == null && x.Element("cr") != null))
            .Select(x =>x.Parent.Attribute("name").Value);

foreach (var message in figs)
{
    Console.WriteLine("Empty <fig> tag with only credit in: {0}", message);
}

有关父级和后代的更多信息,请参见:

https://docs.microsoft.com/zh-cn/dotnet/api/system.xml.linq.xobject.parent?view=netframework-4.7.2

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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