linq - XML Document to IEnumerable<XElement> and get values -


hey guys i'm trying values deeper container in xdocument run in nullreference.

this code far:

                streamreader xmlstream = new streamreader(rsp.getresponsestream());             string xmlstr = xmlstream.readtoend();             xdocument xelement = xdocument.parse(xmlstr);             xmlstream.close();               ienumerable<xelement> items = xelement.elements();              foreach (var item in items )             {                 console.writeline(item.element("itemid").value.tostring());             } 

this xml want work with:

    <getsellerlistresponse xmlns="urn:ebay:apis:eblbasecomponents">   <timestamp>2016-02-26t22:02:38.968z</timestamp>   <ack>success</ack>   <version>967</version>   <build>967_core_bundled_10708779_r1</build>   <paginationresult>     <totalnumberofpages>14</totalnumberofpages>     <totalnumberofentries>27</totalnumberofentries>   </paginationresult>   <hasmoreitems>true</hasmoreitems>   <itemarray>     <item>       <autopay>false</autopay>       <buyerprotection>itemineligible</buyerprotection>       <country>us</country>       <currency>usd</currency>       <hitcounter>nohitcounter</hitcounter>       <itemid>110043597553</itemid>       <listingdetails>         <starttime>2016-02-12t23:35:27.000z</starttime>         <endtime>2016-02-19t23:35:27.000z</endtime>         <viewitemurl>http://cgi.sandbox.ebay.com/ws/ebayisapi.dll?viewitem&            item=110043597553&category=41393</viewitemurl>         <hasunansweredquestions>false</hasunansweredquestions>         <haspublicmessages>false</haspublicmessages>         <buyitnowavailable>true</buyitnowavailable>       </listingdetails>       <listingduration>days_7</listingduration>       <location>santa cruz, california</location>       <primarycategory>         <categoryid>41393</categoryid>         <categoryname>collectibles:decorative collectibles:other</categoryname>       </primarycategory>       <quantity>1</quantity>       <revisestatus>         <itemrevised>false</itemrevised>       </revisestatus>       <secondarycategory>         <categoryid>95116</categoryid>         <categoryname>collectibles:disneyana:contemporary (1968-now):bobblehead figures</categoryname>       </secondarycategory>       <sellingstatus>         <bidcount>0</bidcount>         <bidincrement currencyid="usd">0.5</bidincrement>         <convertedcurrentprice currencyid="usd">11.49</convertedcurrentprice>         <currentprice currencyid="usd">11.49</currentprice>         <minimumtobid currencyid="usd">11.49</minimumtobid>         <quantitysold>0</quantitysold>         <secondchanceeligible>false</secondchanceeligible>         <listingstatus>completed</listingstatus>       </sellingstatus>       <shiptolocations>us</shiptolocations>       <site>us</site>       <storefront>         <storecategoryid>1</storecategoryid>         <storecategory2id>0</storecategory2id>         <storeurl>http://www.stores.sandbox.ebay.com/id=132854966</storeurl>       </storefront>       <subtitle>micky, ears!</subtitle>       <timeleft>pt0s</timeleft>       <title>kelly's kitsch</title>       <watchcount>0</watchcount>       <postalcode>95062</postalcode>       <picturedetails>         <galleryurl>http://thumbs.ebaystatic.com/pict/41007087008080_0.jpg</galleryurl>         <photodisplay>none</photodisplay>         <pictureurl>http://thumbs.ebaystatic.com/pict/41007087008080_0.jpg</pictureurl>       </picturedetails>       <proxyitem>false</proxyitem>       <returnpolicy>         <refundoption>moneyback</refundoption>         <refund>money back</refund>         <returnswithinoption>days_30</returnswithinoption>         <returnswithin>30 days</returnswithin>         <returnsacceptedoption>returnsaccepted</returnsacceptedoption>         <returnsaccepted>returns accepted</returnsaccepted>         <description>returns accepted if item not described.</description>         <shippingcostpaidbyoption>buyer</shippingcostpaidbyoption>         <shippingcostpaidby>buyer</shippingcostpaidby>       </returnpolicy>       <paymentallowedsite>us</paymentallowedsite>     </item>    </itemarray>   <itemsperpage>2</itemsperpage>   <pagenumber>1</pagenumber>   <returneditemcountactual>2</returneditemcountactual> </getsellerlistresponse> 

can please explain missing?

thanks

you're calling elements() on xdocument, that's just going return root element.

you're calling element("itemid") on root element - asking element doesn't exist. return null, leading exception. additionally, you're ignoring fact elements in namespace.

it looks want item elements, use:

// changed variable name match is... xdocument doc = xdocument.parse(xmlstr); xnamespace ns = "urn:ebay:apis:eblbasecomponents"; enumerable<xelement> items = doc.descendants(ns + "item"); foreach (var item in items) {     // xelement.value string; no need call tostring on     console.writeline(item.element(ns + "itemid").value); } 

an alternative find itemarray element, , item children:

xdocument doc = xdocument.parse(xmlstr); xnamespace ns = "urn:ebay:apis:eblbasecomponents"; enumerable<xelement> items = doc.root                                 .element(ns + "itemarray")                                 .elements(ns + "item"); // code before 

Comments