C# 如何查找缺少特定子节点的XML节点?

C# 如何查找缺少特定子节点的XML节点?,c#,.net,xml,linq,linq-to-xml,C#,.net,Xml,Linq,Linq To Xml,此MSDN页面显示如何使用Linq to XML查找包含特定子节点的XML节点。不幸的是,我有一个相反的问题:我有一个大的列表,其中一些节点缺少应该在那里的某个子节点。有什么好办法找到他们吗 例如: <Objects> <Object> <ID>1</ID> <A>1</A> <B>1</B> <C>1</C> </Object&g

此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>

1.
1.
1.
1.
2.
2.
2.
2.
3.
3.
3.
4.
4.
4.

如何设置代码来查找所有缺少
节点的
元素,该节点将返回#3,但不返回#4?

因为
XContainer.Element(“elementName”)
如果
elementName
不存在,则返回
null
(注意:我将您的xml复制到一个名为
xml
的字符串中,因此您只需在
XDocument
上执行


下面是另一个解决方法,如果您想在这种情况下使用
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();
static void Main(字符串[]args)
{
XElement对象=XElement.Parse(@)
1.
1.
1.
1.
2.
2.
2.
2.
3.
3.
3.
4.
4.
4.
");
string xpath_string=“//对象[count(B)=0];//您可以随时更改元素的名称,
//即使在运行时也…只要确保
//要添加'System.Xml.XPath'以利用XPath。。。
IEnumerable查询\u for\u objects=objects.XPathSelectElements(xpath\u字符串);
foreach(查询对象中的元素元素)
{
Console.WriteLine(ele.ToString());
}
Console.ReadKey();
使用
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();