提示:本站收集StackOverFlow近2千万问答,支持中英文搜索,鼠标放在语句上弹窗显示对应的参考中文或英文, 本站还提供 中文繁体 英文版本 中英对照 版本,有任何建议请联系yoyou2525@163.com。
我正在尝试使用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")
,它将为您提供所需的元素。
对于任何VB'ers。
Dim xe As XElement
'some test data
xe = <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>
'get matching email
Dim mtch As String = "email@email.com"
Dim check As List(Of XElement)
check = xe.<property>.<agent>.<agent-email>.Where(Function(el)
Return el.Value = mtch
End Function).ToList
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.