Getting to nested XML tags using Linq to XML?

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I have an XML stream from Amazon that Im trying to get the Item/ItemAttributes/Title from. However, I cant figure out the line in the foreach statement that will get that Element. I get an Object Reference not set error. I have posted the code
and the XML below. Any ideas? Thank you!
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; <<span style="color:#A31515; pre <span style="color:Red; lang<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; x-c#<span style="color:Black; "<span style="color:Blue; >WebRequest request = HttpWebRequest.Create(url);
WebResponse response = request.GetResponse();

//XDocument xdoc = XDocument.Load(response.GetResponseStream());
XElement xelem = XElement.Load(response.GetResponseStream());
XNamespace xn = "http://webservices.amazon.com/AWSECommerceService/2009-03-31";
IEnumerable<span style="color:Blue; <<span style="color:#A31515; XElement<span style="color:Blue; > items = xelem.Elements(xn + "Items");

foreach (var item in items)
{
XML xmlclass = new XML();

<span style="color:Blue; <<span style="color:#A31515; strong<span style="color:Blue; >//this is where i get the object reference not set error...<span style="color:Blue; </<span style="color:#A31515; strong<span style="color:Blue; >
xmlclass.Name = item.Element("Item").Element("ItemAttributes").Element("Title").Value;
}
[/code]
[/code]
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; <<span style="color:#A31515; Items <span style="color:Red; xmlns<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; http://webservices.amazon.com/AWSECommerceService/2009-03-31<span style="color:Black; "<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; Request<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; IsValid<span style="color:Blue; >True<span style="color:Blue; </<span style="color:#A31515; IsValid<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; ItemSearchRequest<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; Condition<span style="color:Blue; >New<span style="color:Blue; </<span style="color:#A31515; Condition<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; DeliveryMethod<span style="color:Blue; >Ship<span style="color:Blue; </<span style="color:#A31515; DeliveryMethod<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; ItemPage<span style="color:Blue; >1<span style="color:Blue; </<span style="color:#A31515; ItemPage<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; MerchantId<span style="color:Blue; >Amazon<span style="color:Blue; </<span style="color:#A31515; MerchantId<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; ResponseGroup<span style="color:Blue; >Medium<span style="color:Blue; </<span style="color:#A31515; ResponseGroup<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; ReviewSort<span style="color:Blue; >-SubmissionDate<span style="color:Blue; </<span style="color:#A31515; ReviewSort<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; SearchIndex<span style="color:Blue; >SportingGoods<span style="color:Blue; </<span style="color:#A31515; SearchIndex<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; Title<span style="color:Blue; >St. Louis Cardinals<span style="color:Blue; </<span style="color:#A31515; Title<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; ItemSearchRequest<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; Request<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; TotalResults<span style="color:Blue; >20519<span style="color:Blue; </<span style="color:#A31515; TotalResults<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; TotalPages<span style="color:Blue; >2052<span style="color:Blue; </<span style="color:#A31515; TotalPages<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; Item<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; ASIN<span style="color:Blue; >B004RQ7Z2Y<span style="color:Blue; </<span style="color:#A31515; ASIN<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; DetailPageURL<span style="color:Blue; >http://www.amazon.com/Sports-Coverage-MLB-MVP-Valance/dp/B004RQ7Z2Y%3FSubscriptionId%3DAKIAIXEO4AM5GSSNUSEA%26tag%3Dws%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3DB004RQ7Z2Y<span style="color:Blue; </<span style="color:#A31515; DetailPageURL<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; ItemLinks<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; ItemLink<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; Description<span style="color:Blue; >Technical Details<span style="color:Blue; </<span style="color:#A31515; Description<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; URL<span style="color:Blue; >http://www.amazon.com/Sports-Coverage-MLB-MVP-Valance/dp/tech-data/B004RQ7Z2Y%3FSubscriptionId%3DAKIAIXEO4AM5GSSNUSEA%26tag%3Dws%26linkCode%3Dxm2%26camp%3D2025%26creative%3D386001%26creativeASIN%3DB004RQ7Z2Y<span style="color:Blue; </<span style="color:#A31515; URL<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; ItemLink<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; ItemLink<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; Description<span style="color:Blue; >Add To Baby Registry<span style="color:Blue; </<span style="color:#A31515; Description<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; URL<span style="color:Blue; >http://www.amazon.com/gp/registry/baby/add-item.html%3Fasin.0%3DB004RQ7Z2Y%26SubscriptionId%3DAKIAIXEO4AM5GSSNUSEA%26tag%3Dws%26linkCode%3Dxm2%26camp%3D2025%26creative%3D386001%26creativeASIN%3DB004RQ7Z2Y<span style="color:Blue; </<span style="color:#A31515; URL<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; ItemLink<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; ItemLink<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; Description<span style="color:Blue; >Add To Wedding Registry<span style="color:Blue; </<span style="color:#A31515; Description<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; URL<span style="color:Blue; >http://www.amazon.com/gp/registry/wedding/add-item.html%3Fasin.0%3DB004RQ7Z2Y%26SubscriptionId%3DAKIAIXEO4AM5GSSNUSEA%26tag%3Dws%26linkCode%3Dxm2%26camp%3D2025%26creative%3D386001%26creativeASIN%3DB004RQ7Z2Y<span style="color:Blue; </<span style="color:#A31515; URL<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; ItemLink<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; ItemLink<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; Description<span style="color:Blue; >Add To Wishlist<span style="color:Blue; </<span style="color:#A31515; Description<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; URL<span style="color:Blue; >http://www.amazon.com/gp/registry/wishlist/add-item.html%3Fasin.0%3DB004RQ7Z2Y%26SubscriptionId%3DAKIAIXEO4AM5GSSNUSEA%26tag%3Dws%26linkCode%3Dxm2%26camp%3D2025%26creative%3D386001%26creativeASIN%3DB004RQ7Z2Y<span style="color:Blue; </<span style="color:#A31515; URL<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; ItemLink<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; ItemLink<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; Description<span style="color:Blue; >Tell A Friend<span style="color:Blue; </<span style="color:#A31515; Description<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; URL<span style="color:Blue; >http://www.amazon.com/gp/pdp/taf/B004RQ7Z2Y%3FSubscriptionId%3DAKIAIXEO4AM5GSSNUSEA%26tag%3Dws%26linkCode%3Dxm2%26camp%3D2025%26creative%3D386001%26creativeASIN%3DB004RQ7Z2Y<span style="color:Blue; </<span style="color:#A31515; URL<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; ItemLink<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; ItemLink<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; Description<span style="color:Blue; >All Customer Reviews<span style="color:Blue; </<span style="color:#A31515; Description<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; URL<span style="color:Blue; >http://www.amazon.com/review/product/B004RQ7Z2Y%3FSubscriptionId%3DAKIAIXEO4AM5GSSNUSEA%26tag%3Dws%26linkCode%3Dxm2%26camp%3D2025%26creative%3D386001%26creativeASIN%3DB004RQ7Z2Y<span style="color:Blue; </<span style="color:#A31515; URL<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; ItemLink<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; ItemLink<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; Description<span style="color:Blue; >All Offers<span style="color:Blue; </<span style="color:#A31515; Description<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; URL<span style="color:Blue; >http://www.amazon.com/gp/offer-listing/B004RQ7Z2Y%3FSubscriptionId%3DAKIAIXEO4AM5GSSNUSEA%26tag%3Dws%26linkCode%3Dxm2%26camp%3D2025%26creative%3D386001%26creativeASIN%3DB004RQ7Z2Y<span style="color:Blue; </<span style="color:#A31515; URL<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; ItemLink<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; ItemLinks<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; SalesRank<span style="color:Blue; >3147<span style="color:Blue; </<span style="color:#A31515; SalesRank<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; ItemAttributes<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; Binding<span style="color:Blue; >Sports<span style="color:Blue; </<span style="color:#A31515; Binding<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; Brand<span style="color:Blue; >Sports Coverage<span style="color:Blue; </<span style="color:#A31515; Brand<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; Label<span style="color:Blue; >Sports Coverage<span style="color:Blue; </<span style="color:#A31515; Label<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; Manufacturer<span style="color:Blue; >Sports Coverage<span style="color:Blue; </<span style="color:#A31515; Manufacturer<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; Model<span style="color:Blue; >03MSVAL3BRA8814-parent<span style="color:Blue; </<span style="color:#A31515; Model<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; ProductGroup<span style="color:Blue; >Apparel<span style="color:Blue; </<span style="color:#A31515; ProductGroup<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; ProductTypeName<span style="color:Blue; >SPORTING_GOODS<span style="color:Blue; </<span style="color:#A31515; ProductTypeName<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; Publisher<span style="color:Blue; >Sports Coverage<span style="color:Blue; </<span style="color:#A31515; Publisher<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; Studio<span style="color:Blue; >Sports Coverage<span style="color:Blue; </<span style="color:#A31515; Studio<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; Title<span style="color:Blue; >Sports Coverage MLB MVP Valance<span style="color:Blue; </<span style="color:#A31515; Title<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; ItemAttributes<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; OfferSummary<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; TotalNew<span style="color:Blue; >0<span style="color:Blue; </<span style="color:#A31515; TotalNew<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; TotalUsed<span style="color:Blue; >0<span style="color:Blue; </<span style="color:#A31515; TotalUsed<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; TotalCollectible<span style="color:Blue; >0<span style="color:Blue; </<span style="color:#A31515; TotalCollectible<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; TotalRefurbished<span style="color:Blue; >0<span style="color:Blue; </<span style="color:#A31515; TotalRefurbished<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; OfferSummary<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; Item<span style="color:Blue; ><span style="color:Blue; </<span style="color:#A31515; Items<span style="color:Blue; >
[/code]

View the full article
 
Back
Top