Click here to Skip to main content
15,888,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
this is the xml listing.xml

using linq to xml it never returns Elements from the query listing.Descendants("StreetName")



here is the code snippet
string queryURI = "http://retsgw.flexmls.com/rets2_0/Search?SearchType=Property&Class=A&QueryType=DMQL2&Query=(LIST_105=4123192)&Count=0&Format=STANDARD-XML&StandardNames=0&RestrictedIndicator=****&Limit=1@Select=LIST_105";
      HttpWebRequest req2 = (HttpWebRequest)WebRequest.Create(queryURI);
      req2.AuthenticationLevel = System.Net.Security.AuthenticationLevel.MutualAuthRequested;
      req2.Credentials = new NetworkCredential("---", "---"); //This line ensures the request is processed through Basic Authentication
      req2.ContentType = "text/xml" ;
      req2.Accept = "text/xml" ;
      req2.Method = "Post" ;
      HttpWebResponse response = (HttpWebResponse)req2.GetResponse();
      StreamReader reader = new StreamReader(response.GetResponseStream());
      string xml = reader.ReadToEnd();


          XDocument listing = XDocument.Parse(xml);
          var streetAddresses = from streetAddress in listing.Descendants("StreetAddress")

                                select new
                                {
                                    streetNumber = streetAddress.Element("StreetNumber").Value,
                                    streetDirPrefix = streetAddress.Element("StreetDirPrefix").Value,
                                    streetName = streetAddress.Element("StreetName").Value,
                                    streetsuffix = streetAddress.Element("StreetSuffix").Value,

                                    City = streetAddress.Element("City").Value,
                                    postalCode = streetAddress.Element("PostalCode").Value
                                };
Posted
Updated 29-Aug-10 10:21am
v2
Comments
Christian Graus 29-Aug-10 16:25pm    
I suspect it would help to see your XML.

1 solution

Finally I was able to find the problem and and fix accordingly (Initially, I thought this was a NameSpace related problem).

You should actually filter the LinQ expression with a condition : where streetAddress.Element("StreetNumber") != null


Here is the correct LinQ that you should use:

C#
var streetAddresses = from streetAddress in listing.Descendants("StreetAddress")
                      where streetAddress.Element("StreetNumber") != null
                      select new
                      {
                          streetNumber = streetAddress.Element("StreetNumber").Value,
                          streetDirPrefix = streetAddress.Element("StreetDirPrefix").Value,
                          streetName = streetAddress.Element("StreetName").Value,
                          streetsuffix = streetAddress.Element("StreetSuffix").Value,
                          City = streetAddress.Element("City").Value,
                          postalCode = streetAddress.Element("PostalCode").Value
                      };


The filtering had to be added because, the listing.Descendants("StreetAddress") actually returns two elements where one element does not have any child element and hence the expression gets null exception.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900