如何找到缺少特定子节点的 XML 节点?

How to find an XML node that is missing a specific child node?

此 MSDN 页面展示了如何使用 Linq-to-XML 查找包含特定子节点的 XML 节点。不幸的是,我遇到了相反的问题:我有一个很大的列表,其中一些节点 缺少 应该存在的某个子节点。有什么好的方法可以找到吗?

例如:

<Objects>
  <Object>
    <ID>1</ID>
    <A>1</A>
    <B>1</B>
    <C>1</C>
  </Object>
  <Object>
    <ID>2</ID>
    <A>2</A>
    <B>2</B>
    <C>2</C>
  </Object>
  <Object>
    <ID>3</ID>
    <A>3</A>
    <C>3</C>
  </Object>
  <Object>
    <ID>4</ID>
    <A>4</A>
    <B/>
    <C>4</C>
  </Object>
</Objects>

我将如何设置代码来查找所有缺少 <B> 节点的 <Object> 元素,return #3,而不是 #4?

这是有效的,因为 XContainer.Element("elementName") returns null 如果 elementName 不存在(注意:我将你的 xml 复制到一个名为 xml, 所以你只需要在你的 XDocument 上做 .Descendents):

    static void Main(string[] args)
    {
        var elementsWithoutB = XDocument.Parse(xml)
                               .Descendants("Object")
                               .Where(x => x.Element("B") == null);

        // Prove it works by printing the elements returned
        foreach (var element in elementsWithoutB)
        {
            Console.WriteLine(element.ToString());
        }

        Console.Read();
    }

这是另一种解决方法,如果您想在这种情况下使用 XPath,您也可以使用以下方法:

static void Main(string[] args)
        {
            XElement objects = XElement.Parse(@"<Objects>
                                                  <Object>
                                                    <ID>1</ID>
                                                    <A>1</A>
                                                    <B>1</B>
                                                    <C>1</C>
                                                  </Object>
                                                  <Object>
                                                    <ID>2</ID>
                                                    <A>2</A>
                                                    <B>2</B>
                                                    <C>2</C>
                                                  </Object>
                                                  <Object>
                                                    <ID>3</ID>
                                                    <A>3</A>
                                                    <C>3</C>
                                                  </Object>
                                                  <Object>
                                                    <ID>4</ID>
                                                    <A>4</A>
                                                    <B/>
                                                    <C>4</C>
                                                  </Object>
                                                </Objects>");

            string xpath_string = "//Object[count(B) = 0]"; //you can change the name of the element anytime,
                                                            //even in the run-time also... just make sure
                                                            //to add `System.Xml.XPath` to utilize the XPath...

            IEnumerable<XElement> query_for_objects = objects.XPathSelectElements(xpath_string);

            foreach (XElement ele in query_for_objects)
            {
                Console.WriteLine(ele.ToString());
            }
            Console.ReadKey();

在这种情况下使用 XPath 有什么好处,您甚至可以在 运行 时使用自定义查询,根本无需更改您的代码!