c# - 带有 WHERE 条件的 XElement LINQ 失败

标签 c# linq xelement

我正在尝试使用 LINQ 从 XML 中获取元素。

XML 文件示例:

<properties>
  <property>
    <location>
      <unit-number>101</unit-number>
      <street-address>123 Main</street-address>
      <city-name>City</city-name>
      <state-code>ST</state-code>
      <zipCode>00000</zipCode>
      <display-address>no</display-address>
    </location>
    <details>
      <property-type>apartment</property-type>
      <price>599.00</price>
      <num-bedrooms>1</num-bedrooms>
      <num-bathrooms>1</num-bathrooms>
      <living-area-square-fee>611</living-area-square-fee>
      <description></description>
      <provider-listingid>819</provider-listingid>
    </details>
    <agent>
      <agent-name>Name</agent-name>
      <agent-email><a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="254048444c49654048444c490b464a48" rel="noreferrer noopener nofollow">[email protected]</a></agent-email>
    </agent>
  </property>
<properties>

我从 Azure 服务器打开 XML(工作正常),并尝试使用 LINQ 来过滤它:

// I have a functiion that loads the XML from blob
// It is working. debug shows the XML in my document variable
XDocument document = XDocument.Load(blob.Uri.AbsoluteUri);

List<XElement> check = (from el in document.Root.Elements("properties").Elements("property").Elements("agent").Elements("agent-email")
                         where el.Value == "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="87e2eae6eeebc7e2eae6eeeba9e4e8ea" rel="noreferrer noopener nofollow">[email protected]</a>"
                               select el).ToList();

// Why this returns null? I have agent-email = <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3e5b535f57527e5b535f5752105d5153" rel="noreferrer noopener nofollow">[email protected]</a>

谢谢

最佳答案

Document.Root properties 元素。删除 .Elements("properties") 它会给你你想要的元素。

关于c# - 带有 WHERE 条件的 XElement LINQ 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53305626/

相关文章:

c# - 如何在 C#/.NET 中声明常量?

c# - 无法转换类型为 'System.Data.EnumerableRowCollection` 的对象 1[System.Int32 ]' to type ' System.IConvertible'

c# - 为什么此 LINQ 分组的计数为 3 而不是 2?

c# - 使用 String.IsNullOrEmpty(string) 和 Nhibernate 创建动态 Linq 表达式

c# - 用ID修改节点值C#

c# - XElement Nodes() 与 Elements() 有什么区别?

c# - 如何改进这个简单的模板渲染类?

c# 如何获取我的列表框项并在我按下检查列表框时快速添加它

c# - 创建帐户状态为 "CONFIRMED"且没有电子邮件地址的 AWS Cognito 用户

c# - 将任何用户输入添加为 XML 文档中的节点