0

I am trying to read an XML file using XDocument and I am getting System.NullReferenceException' error while running the program.

The Xml code is

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<feed xml:base="http://pinpoint.microsoft.com/en-US/syndicate/" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://www.w3.org/2005/Atom">
<title type="text">Partners</title>
<id>http://google.com/en-US/syndicate/Partners</id>
<updated>2014-01-20T12:29:25Z</updated>
 <link rel="self" title="Partners" href="Partners" />
 <entry>
 <id>http://google.com/en-US/syndicate/Partners('429')</id>
 <title type="text">Light Systems</title>
 <summary type="text">How may we help you? will help you address concerns like security and productivity that may be delaying your transition to a work anywhere, anytime workforce. Mac, Unix and Linux platforms. Whatever your size or need, we have an IT plan that will work for your business.
 </summary>
 <published>2009-07-27T14:23:39-07:00</published>
 <updated>2014-01-10T15:00:41-08:00</updated>
 <author>
  <name>Google Production</name>
  <uri>http://google.com/</uri>
  <email>calamity@google.com</email>
 </author>
 <link rel="edit" title="Partner" href="Partners('429')" />
 <link rel="http://google.com/ado/2007/08/dataservices/related/Links" type="application/atom+xml;type=feed" title="Links" href="Partners('429')/Links">
   <m:inline>
    <feed>
      <title type="text">Links</title>
      <id>http://google.com/en-US/syndicate/Partners('429')/Links</id>
      <updated>2014-01-20T12:29:25Z</updated>
      <link rel="self" title="Links" href="Partners('429')/Links" />
      <entry>
        <id>http://google.com/en-US/syndicate/Links('partnerpage')</id>
        <title type="text">
        </title>
        <updated>2014-01-20T12:29:25Z</updated>
        <author>
          <name />
        </author>
        <link rel="edit" title="Link" href="Links('partnerpage')" />
        <category term="Google.Commerce.Marketplace.Syndicate.V2010_05.Link" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
        <content type="application/xml">
          <m:properties>
            <d:Type>partnerpage</d:Type>
            <d:Description>Partner Page</d:Description>
            <d:Url>http://google.com/en-US/PartnerDetails.aspx?PartnerId=429&amp;wt.mc_id=ht43</d:Url>
          </m:properties>
        </content>
      </entry>
      <entry>
        <id>http://google.com/en-US/syndicate/Links('partnerproductlist')</id>
        <title type="text">
        </title>
        <updated>2014-01-20T12:29:25Z</updated>
        <author>
          <name />
        </author>
        <link rel="edit" title="Link" href="Links('partnerproductlist')" />
        <category term="Google.Commerce.Marketplace.Syndicate.V2010_05.Link" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
        <content type="application/xml">
          <m:properties>
            <d:Type>partnerproductlist</d:Type>
            <d:Description>Partner Product List</d:Description>
            <d:Url>http://google.com/en-US/PartnerDetails.aspx?partnerId=429&amp;CurrentTab=1&amp;wt.mc_id=ht43</d:Url>
          </m:properties>
        </content>
      </entry>
      <entry>
        <id>http://google.com/en-US/syndicate/Links('partnerreviewpage')</id>
        <title type="text">
        </title>
        <updated>2014-01-20T12:29:25Z</updated>
        <author>
          <name />
        </author>
        <link rel="edit" title="Link" href="Links('partnerreviewpage')" />
        <category term="Google.Commerce.Marketplace.Syndicate.V2010_05.Link" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
        <content type="application/xml">
          <m:properties>
            <d:Type>partnerreviewpage</d:Type>
            <d:Description>Partner Review Page</d:Description>
            <d:Url>http://google.com/en-US/PartnerDetails.aspx?partnerId=429&amp;CurrentTab=2&amp;wt.mc_id=ht43</d:Url>
          </m:properties>
        </content>
      </entry>
      <entry>
        <id>http://google.com/en-US/syndicate/Links('partnerlocationpage')</id>
        <title type="text">
        </title>
        <updated>2014-01-20T12:29:25Z</updated>
        <author>
          <name />
        </author>
        <link rel="edit" title="Link" href="Links('partnerlocationpage')" />
        <category term="Google.Commerce.Marketplace.Syndicate.V2010_05.Link" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
        <content type="application/xml">
          <m:properties>
            <d:Type>partnerlocationpage</d:Type>
            <d:Description>Partner Location Page</d:Description>
            <d:Url>http://google.com/en-US/PartnerDetails.aspx?partnerId=429&amp;CurrentTab=3&amp;wt.mc_id=ht43</d:Url>
          </m:properties>
        </content>
      </entry>
      <entry>
        <id>http://google.com/en-US/syndicate/Links('partnerrfipage')</id>
        <title type="text">
        </title>
        <updated>2014-01-20T12:29:25Z</updated>
        <author>
          <name />
        </author>
        <link rel="edit" title="Link" href="Links('partnerrfipage')" />
        <category term="Google.Commerce.Marketplace.Syndicate.V2010_05.Link" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
        <content type="application/xml">
          <m:properties>
            <d:Type>partnerrfipage</d:Type>
            <d:Description>Partner RFI Page</d:Description>
            <d:Url>http://google.microsoft.com/en-US/RFI.aspx?partnerId=429&amp;wt.mc_id=ht43</d:Url>
          </m:properties>
        </content>
      </entry>
      <entry>
        <id>http://google.com/en-US/syndicate/Links('partnerwebsite')</id>
        <title type="text">
        </title>
        <updated>2014-01-20T12:29:25Z</updated>
        <author>
          <name />
        </author>
        <link rel="edit" title="Link" href="Links('partnerwebsite')" />
        <category term="Google.Commerce.Marketplace.Syndicate.V2010_05.Link" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
        <content type="application/xml">
          <m:properties>
            <d:Type>partnerwebsite</d:Type>
            <d:Description>Website</d:Description>
            <d:Url>http://google.com/en-US/syndicate/relay/partner/website?id=429&amp;link=http%3a%2f%2fwww.systems.com&amp;TrackID=ht43</d:Url>
          </m:properties>
        </content>
      </entry>
      <entry>
        <id>http://google.com/en-US/syndicate/Links('partnerlogo')</id>
        <title type="text">
        </title>
        <updated>2014-01-20T12:29:25Z</updated>
        <author>
          <name />
        </author>
        <link rel="edit" title="Link" href="Links('partnerlogo')" />
        <category term="Microsoft.Commerce.Marketplace.Syndicate.V2010_05.Link" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
        <content type="application/xml">
          <m:properties>
            <d:Type>partnerlogo</d:Type>
            <d:Description>Logo</d:Description>
            <d:Url>http://www.systems.com/lan_logo.jpg</d:Url>
          </m:properties>
        </content>
      </entry>
    </feed>
  </m:inline>
</link>
<link rel="http://google.com/ado/2007/08/dataservices/related/Locations" type="application/atom+xml;type=feed" title="Locations" href="Partners('429')/Locations">
  <m:inline>
    <feed>
      <title type="text">Locations</title>
      <id>http://google.com/en-US/syndicate/Partners('429')/Locations</id>
      <updated>2014-01-20T12:29:25Z</updated>
      <link rel="self" title="Locations" href="Partners('429')/Locations" />
      <entry>
        <id>http://google.com/en-US/syndicate/Locations(1)</id>
        <title type="text">
        </title>
        <updated>2014-01-20T12:29:25Z</updated>
        <author>
          <name />
        </author>
        <link rel="edit" title="Location" href="Locations(1)" />
        <category term="Google.Commerce.Marketplace.Syndicate.V2010_05.Location" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
        <content type="application/xml">
          <m:properties>
            <d:Id m:type="Edm">1</d:Id>
            <d:Address>Crossing Park, Suite E</d:Address>
            <d:City>Norwalk</d:City>
            <d:State>CT</d:State>
            <d:Country>USA</d:Country>
            <d:PostalCode>30071</d:PostalCode>
            <d:HQ m:type="Edm.Boolean">true</d:HQ>
            <d:Latitude m:type="Edm.Double">566</d:Latitude>
            <d:Longitude m:type="Edm.Double">-56.56</d:Longitude>
            <d:ContactName m:null="true" />
            <d:ContactPhoneNumber>+1 9175391100</d:ContactPhoneNumber>
            <d:ContactPhoneExtension m:null="true" />
            <d:ContactFaxNumber>+1 9175391100</d:ContactFaxNumber>
          </m:properties>
        </content>
      </entry>
      </feed>
    </m:inline>
    </link>
     <link rel="http://google.com/ado/2007/08/dataservices/related/Products" type="application/atom+xml;type=feed" title="Products" href="Partners('429')/Products" />
     <link rel="http://google.com/ado/2007/08/dataservices/related/Categories" type="application/atom+xml;type=feed" title="Categories" href="Partners('429')/Categories" />
    <category term="google.Marketplace.Syndicate.V2010_05.Partner" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
   <content type="application/xml">
     <m:properties>
    <d:View>v244</d:View>
    <d:Id>429</d:Id>
     <d:NativeId>630</d:NativeId>
     <d:Name>Systems</d:Name>
      <d:Description xml:space="preserve">will help </d:Description>
        <d:RatingAverage m:type="Edm.Decimal">555</d:RatingAverage>
       <d:RatingCount m:type="Edm.Int32">56</d:RatingCount>
      <d:Certification>Registered Member</d:Certification>
     <d:Search m:null="true" />
     <d:Loc m:null="true" />
     <d:Dist m:null="true" />
     <d:Cat m:null="true" />
   </m:properties>
  </content>
  <rights type="text">Copyright &amp;copy; 2011 Google Corporation</rights>
  </entry>
 <entry>
  <id>http://google.com/en-US/syndicate/Partners('4294')</id>
  <title type="text">Wholesale Intl.</title>
    <summary type="text">SQL Server </summary>
    <published>2009-07-21T14:23:50-07:00</published>
    <updated>2013-08-20T02:17:21-07:00</updated>
   <author>
   <name>google</name>
   <uri>http://google.com/</uri>
   <email>gmail.com</email>
   </author>
   <link rel="edit" title="Partner" href="Partners('4294')" />
   <link rel="http://google.com/ado/2007/08/dataservices/related/Links" type="application/atom+xml;type=feed" title="Links" href="Partners('4294')/Links">
     <m:inline>
    <feed>
      <title type="text">Links</title>
      <id>http://google.com/en-US/syndicate/Partners('4294')/Links</id>
      <updated>2014-01-20T12:29:25Z</updated>
      <link rel="self" title="Links" href="Partners('4294')/Links" />
      <entry>
        <id>http://google.com/en-US/syndicate/Links('partnerpage')</id>
        <title type="text">
        </title>
        <updated>2014-01-20T12:29:25Z</updated>
        <author>
          <name />
        </author>
        <link rel="edit" title="Link" href="Links('partnerpage')" />
        <category term="Google.Commerce.Marketplace.Syndicate.V2010_05.Link" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
        <content type="application/xml">
          <m:properties>
            <d:Type>partnerpage</d:Type>
            <d:Description>Page</d:Description>
            <d:Url>http://google.microsoft.com/en-US/PartnerDetails.aspx?PartnerId=4294&amp;wt.mc_id=ht43</d:Url>
          </m:properties>
          </content>
        </entry>
      <entry>
        <id>http://google.com/en-US/syndicate/Links('partnerproductlist')</id>
        <title type="text">
        </title>
        <updated>2014-01-20T12:29:25Z</updated>
        <author>
          <name />
        </author>
        <link rel="edit" title="Link" href="Links('partnerproductlist')" />
        <category term="Google.Commerce.Marketplace.Syndicate.V2010_05.Link" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
        <content type="application/xml">
          <m:properties>
            <d:Type>partnerproductlist</d:Type>
            <d:Description>Product List</d:Description>
            <d:Url>http://google.com/en-US/PartnerDetails.aspx?partnerId=4294&amp;CurrentTab=1&amp;wt.mc_id=ht43</d:Url>
          </m:properties>
        </content>
      </entry>
      <entry>
        <id>http://google.com/en-US/syndicate/Links('partnerreviewpage')</id>
        <title type="text">
        </title>
        <updated>2014-01-20T12:29:25Z</updated>
        <author>
          <name />
        </author>
        <link rel="edit" title="Link" href="Links('pinpointpartnerreviewpage')" />
        <category term="Google.Marketplace.Syndicate.V2010_05.Link" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
        <content type="application/xml">
          <m:properties>
            <d:Type>partnerreviewpage</d:Type>
            <d:Description>Review Page</d:Description>
            <d:Url>http://google.com/en-US/PartnerDetails.aspx?partnerId=4294&amp;CurrentTab=2&amp;wt.mc_id=ht43</d:Url>
          </m:properties>
        </content>
       </entry>
       <entry>
        <id>http://google.com/en-US/syndicate/Links('partnerlocationpage')</id>
        <title type="text">
        </title>
        <updated>2014-01-20T12:29:25Z</updated>
        <author>
          <name />
        </author>
        <link rel="edit" title="Link" href="Links('partnerlocationpage')" />
        <category term="google.Marketplace.Syndicate.V2010_05.Link" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
        <content type="application/xml">
          <m:properties>
            <d:Type>partnerlocationpage</d:Type>
            <d:Description>Location Page</d:Description>
            <d:Url>http://google.com/en-US/PartnerDetails.aspx?partnerId=4294&amp;CurrentTab=3&amp;wt.mc_id=ht43</d:Url>
          </m:properties>
        </content>
      </entry>
      <entry>
        <id>http://google.com/en-US/syndicate/Links('partnerrfipage')</id>
        <title type="text">
        </title>
        <updated>2014-01-20T12:29:25Z</updated>
        <author>
          <name />
        </author>
        <link rel="edit" title="Link" href="Links('partnerrfipage')" />
        <category term="Google.Marketplace.Syndicate.V2010_05.Link" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
        <content type="application/xml">
          <m:properties>
            <d:Type>partnerrfipage</d:Type>
            <d:Description>RFI Page</d:Description>
            <d:Url>http://pinpoint.microsoft.com/en-US/RFI.aspx?partnerId=4294&amp;wt.mc_id=ht43</d:Url>
          </m:properties>
        </content>
        </entry>
        <entry>
        <id>http://google.com/en-US/syndicate/Links('partnerwebsite')</id>
        <title type="text">
        </title>
        <updated>2014-01-20T12:29:25Z</updated>
        <author>
          <name />
        </author>
        <link rel="edit" title="Link" href="Links('partnerwebsite')" />
        <category term="google.Marketplace.Syndicate.V2010_05.Link" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
        <content type="application/xml">
          <m:properties>
            <d:Type>partnerwebsite</d:Type>
            <d:Description>Website</d:Description>
            <d:Url>http://google.com/en-US/syndicate/relay/partner/website?id=4294&amp;link=http%3a%2f%2fwww.software-intl.com&amp;TrackID=ht43</d:Url>
          </m:properties>
        </content>
      </entry>
      <entry>
        <id>http://google.com/en-US/syndicate/Links('partnerlogo')</id>
        <title type="text">
        </title>
        <updated>2014-01-20T12:29:25Z</updated>
        <author>
          <name />
        </author>
        <link rel="edit" title="Link" href="Links('partnerlogo')" />
        <category term="Google.Commerce.Marketplace.Syndicate.V2010_05.Link" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
        <content type="application/xml">
          <m:properties>
            <d:Type>partnerlogo</d:Type>
            <d:Description>Logo</d:Description>
            <d:Url>http://www.software-intl.com/uploads/2012/04/swi-logo-web.png</d:Url>
          </m:properties>
        </content>
      </entry>
       </feed>
     </m:inline>
   </link>
   <link rel="http://google.com/ado/2007/08/dataservices/related/Locations" type="application/atom+xml;type=feed" title="Locations" href="Partners('4294')/Locations">
     <m:inline>
    <feed>
      <title type="text">Locations</title>
      <id>http://google.com/en-US/syndicate/Partners('4294')/Locations</id>
      <updated>2014-01-20T12:29:25Z</updated>
      <link rel="self" title="Locations" href="Partners('4294')/Locations" />
      <entry>
        <id>http://google.com/en-US/syndicate/Locations(1)</id>
        <title type="text">
        </title>
        <updated>2014-01-20T12:29:25Z</updated>
        <author>
          <name />
        </author>
        <link rel="edit" title="Location" href="Locations(1)" />
        <category term="Google.Marketplace.Syndicate.V2010_05.Location" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
        <content type="application/xml">
          <m:properties>
            <d:Id m:type="Edm">1</d:Id>
            <d:Address>1114</d:Address>
            <d:City>Longt</d:City>
            <d:State>CT</d:State>
            <d:Country>USA</d:Country>
            <d:PostalCode>80455</d:PostalCode>
            <d:HQ m:type="Edm.Boolean">true</d:HQ>
            <d:Latitude m:type="Edm.Double">4455</d:Latitude>
            <d:Longitude m:type="Edm.Double">444</d:Longitude>
            <d:ContactName>xxxx</d:ContactName>
            <d:ContactPhoneNumber>+1 917000000</d:ContactPhoneNumber>
            <d:ContactPhoneExtension m:null="true" />
            <d:ContactFaxNumber>+1 917000000</d:ContactFaxNumber>
          </m:properties>
        </content>
      </entry>
      <entry>
        <id>http://google.com/en-US/syndicate/Locations(2)</id>
        <title type="text">
        </title>
        <updated>2014-01-20T12:29:25Z</updated>
        <author>
          <name />
        </author>
        <link rel="edit" title="Location" href="Locations(2)" />
        <category term="google.Marketplace.Syndicate.V2010_05.Location" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
        <content type="application/xml">
          <m:properties>
            <d:Id m:type="Edm">2</d:Id>
            <d:Address>Suite 206</d:Address>
            <d:City>Moo</d:City>
            <d:State>CT</d:State>
            <d:Country>USA</d:Country>
            <d:PostalCode>88154</d:PostalCode>
            <d:HQ m:type="Edm.Boolean">false</d:HQ>
            <d:Latitude m:type="Edm.Double">32.01</d:Latitude>
            <d:Longitude m:type="Edm.Double">-333</d:Longitude>
            <d:ContactName>cddc</d:ContactName>
            <d:ContactPhoneNumber>1-917000000</d:ContactPhoneNumber>
            <d:ContactPhoneExtension>457</d:ContactPhoneExtension>
            <d:ContactFaxNumber>917000000</d:ContactFaxNumber>
          </m:properties>
        </content>
      </entry>
      </feed>
    </m:inline>
   </link>
    <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Products" type="application/atom+xml;type=feed" title="Products" href="Partners('4294')/Products" />
   <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Categories" type="application/atom+xml;type=feed" title="Categories" href="Partners('4294')/Categories" />
   <category term="Google.Commerce.Marketplace.Syndicate.V2010_05.Partner" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
   <content type="application/xml">
  <m:properties>
    <d:View>v45</d:View>
    <d:Id>4294</d:Id>
    <d:NativeId>7104</d:NativeId>
    <d:Name>Wholesale Intl.</d:Name>
    <d:Description> license or a comprehensive company-wide license agreement we can assist you to license your Microsoft software. </d:Description>
    <d:RatingAverage m:type="Edm.Decimal">455</d:RatingAverage>
    <d:RatingCount m:type="Edm.Int32">55</d:RatingCount>
    <d:Certification>hagh</d:Certification>
    <d:Search m:null="true" />
    <d:Loc m:null="true" />
    <d:Dist m:null="true" />
    <d:Cat m:null="true" />
  </m:properties>
   </content>
  <rights type="text">Copyright &amp;copy; 2011 Google Corporation</rights>
 </entry>
</feed>

The piece of code used to read the XML file

   try
        {
            // Another method
            var reader = new  StreamReader("C:/Users/Administrator/Downloads/direct.xml");

            var xmlDoc1 = XDocument.Load(reader);

            XNamespace atom = "http://www.w3.org/2005/Atom";
            XNamespace metadata = "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata";
            XNamespace dataservices = "http://schemas.microsoft.com/ado/2007/08/dataservices";

            var result = xmlDoc1.Root.Elements(atom + "entry")
                .Select(e => new
                {
                    Title = e.Element(atom + "title").Value,
                    Id = e.Element(atom + "id").Value,
                    Urls = e.Elements(atom + "link")
                        .Where(l => l.Element(metadata + "inline") != null)
                        .SelectMany(l => l.Element(metadata + "inline")
                            .Element(atom + "feed")
                            .Elements(atom + "entry")
                            .Select(e1 => e1.Element(atom + "content")
                                .Element(metadata + "properties")
                                .Element(dataservices + "Url").Value))
                });

            //foreach (var item in result)
            //{
            //  Debug.WriteLine("{0},{1},{2}", item.Title,item.Id,String.Join(",",item.Urls));

            //}
            foreach (var entry in result)
            {
                Debug.WriteLine("{0},{1},{2}", entry.Title, entry.Id, String.Concat(entry.Urls));
            }
        }
        catch (Exception e)
        {
            Debug.WriteLine("There is an error :" + e);
        }

The error is as follows - A first chance exception of type 'System.NullReferenceException' occurred in Microsoft.Pinpoint.Samples.exe There is an error :System.NullReferenceException: Object reference not set to an instance of an object.

The full Xml has been posted for reference. I am using XDocument to read the XML file and trying get the information as

 1.entry
   --title
 2.entry
    --id
 3.entry 
   --link
    --inline
      --feed
        --entry
          --content
            --properties
             --Url

Also Trying to read field 'City' not mentioned in the code as

   /entry/link/inline/feed/entry/content/properties/City

and NativeId as

   /entry/content/properties/NativeId

New to C# programming and trying to get the program up and running. My desired output is

 title,id,Url,City,NativeId

like for example

  Light Systems,http://google.com/en-US/syndicate/Partners('429'),http://google.com/en-US/PartnerDetails.aspx?PartnerId=429&amp;wt.mc_id=ht43,Norwalk,429,630
  Wholesale Intl.,http://google.com/en-US/syndicate/Partners('4294'),http://google.microsoft.com/en-US/PartnerDetails.aspx?PartnerId=4294&amp;wt.mc_id=ht43,Longt,4294,7104
  Wholesale Intl.,http://google.com/en-US/syndicate/Partners('4294'),http://google.microsoft.com/en-US/PartnerDetails.aspx?PartnerId=4294&amp;wt.mc_id=ht43,Moo,4294,7104

I have posted the whole XMl file and the whole code for reference. Hope so it helps.

The Exception is happening at -

 e1.Element(atom + "content")
                                .Element(metadata + "properties")
                                .Element(dataservices + "Url").Value))

under the 'Value' it is stating

  NullreferenceException
  Object reference not set to an instance of an object.'
user3188390
  • 555
  • 2
  • 8
  • 17
  • 2
    Almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders Jan 20 '14 at 20:31
  • @JeffMercado Yes, there is an exception, which is Caught by `try`/`catch` and just printed into your `Debug` stream. – MarcinJuraszek Jan 20 '14 at 20:41
  • @MarcinJuraszek: Does it? I copied the query code (without the try/catch blocks) and threw it into LINQPad and got no exceptions running it. – Jeff Mercado Jan 20 '14 at 20:46
  • @MarcinJuraszek: Oh wait... the `Urls` only show the exception... LINQPad contained the error from crashing everything... nevermind. – Jeff Mercado Jan 20 '14 at 20:47

1 Answers1

3

You're getting the exception because not all entry elements contains content/properties/Url element.

Use (string)XElement instead of XElement.Value for to get null when element does not exist instead of NullReferenceException.

var result = xmlDoc1.Root.Elements(atom + "entry")
    .Select(e => new
    {
        Title = (string)e.Element(atom + "title"),
        Id = (string)e.Element(atom + "id"),
        Urls = e.Elements(atom + "link")
            .Where(l => l.Element(metadata + "inline") != null)
            .SelectMany(l => l.Element(metadata + "inline")
                .Element(atom + "feed")
                .Elements(atom + "entry")
                .Select(e1 => (string)e1.Element(atom + "content")
                    .Element(metadata + "properties")
                    .Element(dataservices + "Url")))
    });

You can add additional Where(u => u != null) to remove these null from your result collection:

var result = xmlDoc1.Root.Elements(atom + "entry")
    .Select(e => new
    {
        Title = (string)e.Element(atom + "title"),
        Id = (string)e.Element(atom + "id"),
        Urls = e.Elements(atom + "link")
            .Where(l => l.Element(metadata + "inline") != null)
            .SelectMany(l => l.Element(metadata + "inline")
                .Element(atom + "feed")
                .Elements(atom + "entry")
                .Select(e1 => (string)e1.Element(atom + "content")
                    .Element(metadata + "properties")
                    .Element(dataservices + "Url"))
                .Where(u => u != null))
    });
MarcinJuraszek
  • 118,129
  • 14
  • 170
  • 241
  • +1 because I just learned I don't have to use my common extension method for missing elements, values, or even attribute values. Thanks a lot man! Of course a cast to `string` does exactly what I want! – Mike Perrenoud Jan 20 '14 at 20:39
  • @MarcinJuraszek: How will I then get each of the values of Url as desired in the output? – user3188390 Jan 20 '14 at 20:42
  • @ MarcinJuraszek- Worked like a charm Thanks dude for the information , after a long struggle I finally got the answer, sometimes worth the wait – user3188390 Jan 20 '14 at 20:55