无法将linq转换为xml以读取我的xml文档 [英] cant get linq to xml to read my xml doc

查看:88
本文介绍了无法将linq转换为xml以读取我的xml文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是xml listing.xml

使用linq到xml时,它永远不会从查询列表中返回元素.Descendants("StreetName")



这是代码片段

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

推荐答案

最后,我能够找到问题并进行相应的修复(最初,我认为这是与NameSpace相关的问题).

您实际上应该使用以下条件过滤LinQ表达式:where streetAddress.Element("StreetNumber") != null


这是您应该使用的正确的LinQ:

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:

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



必须添加过滤条件,因为listing.Descendants("StreetAddress")实际上返回了两个元素,其中一个元素没有任何子元素,因此表达式获得了null异常.



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.


这篇关于无法将linq转换为xml以读取我的xml文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆