Search code examples
c#linqxelement

XElement LINQ with WHERE condition fails


I'm trying to get an element from my XML using LINQ.

XML file example:

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

I open my XML from Azure server (working fine) and I try to use LINQ to filter it:

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

Thanks


Solution

  • For any 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