【问题标题】:Filter result with certain node in XML使用 XML 中的某个节点过滤结果
【发布时间】:2019-06-20 11:34:25
【问题描述】:

我有一个像下面这样的xml

<Orders>
<Order>
<ABC>defe</ABC>
</Order>

<Order>
<ABC>asa</ABC>
<YYY>ee1@ee.com</YYY>
</Order>

<Order>
<ABC>tyty</ABC>
<YYY>ee1@ee.com</YYY>
</Order>
</Orders>

我想取出具有节点 YYY 的记录,就像在上面的情况下,查询应该返回两组顺序,其中包含 YYY 节点。第一条记录应该从过滤结果中排除。 结果应该是

<Order>
<ABC>asa</ABC>
<YYY>ee1@ee.com</YYY>
</Order>

<Order>
<ABC>tyty</ABC>
<YYY>ee1@ee.com</YYY>
</Order>

请帮帮我。

【问题讨论】:

标签: c# xml linq-to-xml


【解决方案1】:

见下面的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication116
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);

            List<XElement> orders = doc.Descendants("Order").Where(x => x.Element("YYY") != null).ToList();
        }
    }
}

【讨论】:

  • 天哪,就是这么简单,我尝试使用 contains 但没有成功,谢谢
  • 如果对你有帮助,你能把他的答案标记为correct吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-27
相关资源
最近更新 更多