More Troubles with Linq to Xml

R

Roshawn

Hi,

I'm having a slight problem obtaining nodes from an xml file. Here's sample of what the
file looks like:

<?xml version="1.0" encoding="UTF-8"?>
<ItemSearchResponse>
<OperationRequest>
<HTTPHeaders>
<Header Name="UserAgent" Value="Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US;
rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14">
</Header>
</HTTPHeaders>
</OperationRequest>
<Items>
<Item>
<ASIN>B000VFN934</ASIN>
<SmallImage>
<URL>http://ecx.images-amazon.com/images/I/419iznuXv1L._SL75_.jpg</URL>
<Height Units="pixels">75</Height>
<Width Units="pixels">75</Width>
</SmallImage>
<ItemAttributes>
<ListPrice>
<Amount>21800</Amount>
<CurrencyCode>USD</CurrencyCode>
<FormattedPrice>$218.00</FormattedPrice>
</ListPrice>
<Title>Arturo Italian Leather Triple Compartment Tote</Title>
</ItemAttributes>
</Item>
<Item>
<ASIN>B001534CZA</ASIN>
<SmallImage>
<URL>http://ecx.images-amazon.com/images/I/51Nw9o2xGGL._SL75_.jpg</URL>
<Height Units="pixels">41</Height>
<Width Units="pixels">75</Width>
</SmallImage>
<ItemAttributes>
<ListPrice>
<Amount>3600</Amount>
<CurrencyCode>USD</CurrencyCode>
<FormattedPrice>$36.00</FormattedPrice>
</ListPrice>
<Title>La Regale Satin Beaded Shoulder Clutch</Title>
</ItemAttributes>
</Item>
<Item>
<ASIN>B000VFS7UY</ASIN>
<SmallImage>
<URL>http://ecx.images-amazon.com/images/I/41B2pi7FRgL._SL75_.jpg</URL>
<Height Units="pixels">75</Height>
<Width Units="pixels">75</Width>
</SmallImage>
<ItemAttributes>
<ListPrice>
<Amount>3200</Amount>
<CurrencyCode>USD</CurrencyCode>
<FormattedPrice>$32.00</FormattedPrice>
</ListPrice>
<Title>La Regale Beaded Satin Flap Clutch</Title>
</ItemAttributes>
</Item>
<Item>
<ASIN>B000090LU3</ASIN>
<SmallImage>
<URL>http://ecx.images-amazon.com/images/I/31BN7EGXaLL._SL75_.jpg</URL>
<Height Units="pixels">75</Height>
<Width Units="pixels">75</Width>
</SmallImage>
<ItemAttributes>
<Title>Fontanelli Heart Shape Italian Polished Leather Handbag</Title>
</ItemAttributes>
<OfferSummary>
<LowestNewPrice>
<Amount>21300</Amount>
<CurrencyCode>USD</CurrencyCode>
<FormattedPrice>$213.00</FormattedPrice>
</LowestNewPrice>
<TotalNew>1</TotalNew>
<TotalUsed>0</TotalUsed>
<TotalCollectible>0</TotalCollectible>
<TotalRefurbished>0</TotalRefurbished>
</OfferSummary>
<Offers>
<Offer>
<OfferListing>
<Price>
<Amount>21300</Amount>
<CurrencyCode>USD</CurrencyCode>
<FormattedPrice>$213.00</FormattedPrice>
</Price>
</OfferListing>
</Offer>
</Offers>
<Variations>
<!-- other nodes go here -->
</Variations>
</Item>
<Item>
<ASIN>B000PDE3CS</ASIN>
<SmallImage>
<URL>http://ecx.images-amazon.com/images/I/41tb9PDh5aL._SL75_.jpg</URL>
<Height Units="pixels">75</Height>
<Width Units="pixels">75</Width>
</SmallImage>
<ItemAttributes>
<ListPrice>
<Amount>6000</Amount>
<CurrencyCode>USD</CurrencyCode>
<FormattedPrice>$60.00</FormattedPrice>
</ListPrice>
<Title>LimeNRoses Horseshoe Hobo</Title>
</ItemAttributes>
</Item>
<Item>
<ASIN>B0002ZBG3I</ASIN>
<SmallImage>
<URL>http://ecx.images-amazon.com/images/I/41UND7lqF9L._SL75_.jpg</URL>
<Height Units="pixels">75</Height>
<Width Units="pixels">75</Width>
</SmallImage>
<ItemAttributes>
<Title>Fontanelli Black Stitched Soft Leather Tote</Title>
</ItemAttributes>
<OfferSummary>
<LowestNewPrice>
<Amount>39200</Amount>
<CurrencyCode>USD</CurrencyCode>
<FormattedPrice>$392.00</FormattedPrice>
</LowestNewPrice>
<TotalNew>1</TotalNew>
<TotalUsed>0</TotalUsed>
<TotalCollectible>0</TotalCollectible>
<TotalRefurbished>0</TotalRefurbished>
</OfferSummary>
<Offers>
<Offer>
<OfferListing>
<Price>
<Amount>39200</Amount>
<CurrencyCode>USD</CurrencyCode>
<FormattedPrice>$392.00</FormattedPrice>
</Price>
</OfferListing>
</Offer>
</Offers>
<Variations>
<Item>
<!-- other nodes go here -->
</Item>
</Variations>
</Item>
<!-- more Item Nodes go here -->
</Items>
</ItemSearchResponse>

I'm getting selected nodes from the file like this:

Dim responseDoc As XDocument = XDocument.Load("..\..\XMLFile1.xml")
Dim query = From nd In responseDoc...<Item> _
Select New With { _
.ASIN = nd.<ASIN>.Value, _
.Image = nd.<SmallImage>.<URL>.Value, _
.Title = nd.<ItemAttributes>.<Title>.Value}


I want to ignore all Item nodes that are contained within the Variations node, but my code
doesn't do that. It gets all Item nodes regardless of where they are. How can I do this?

Thanks,
Roshawn
 
R

Roshawn

Hi guys and gals,

I think I solved my problem. By adding a Where clause (i.e. "Where
nd.Parent.Name.LocalName = "Item"), I was able to get my code to behave like I want it to.

Thanks for your consideration,
Roshawn :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top