c# - 带有 WHERE 条件的 XElement LINQ 失败
问题描述
我正在尝试使用 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>email@email.com</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 == "email@email.com"
select el).ToList();
// Why this returns null? I have agent-email = email@email.com
谢谢
解决方案
Document.Root
是properties
元素。删除.Elements("properties")
它会给你你想要的元素。
推荐阅读
- python - 分配数据框的列值,其中两个数据框的列相等
- r - 如何正确形成ggplot图,而不切断图的重要部分?
- javascript - 重新加载时下拉选择重新加载到第一个选项
- mongodb - 在 MongoDB 中从周日到一周前传递数据
- c++ - 在 C++ 中每次运行时获得相同的随机双精度数
- python - 在 tensorflow 2.x 中替代 with_grads 以在 tensorboard 上记录梯度
- javascript - 如何在 laravel 中使用 ajax 更改数据库中的状态
- android-studio - 删除 .zsh_history 文件安全吗?
- javascript - “addEventListener”不适用于 Chrome 扩展
- python - 如何使用 pyodbc 发出包含 python 变量作为参数的 SQL 查询?