473,387 Members | 1,463 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Need Help with Linq to Xml

Hi,

My usage of Linq to Xml has been going great until now. I'm getting an error whenever I
don't find a particular node in the xml, even though I use an auxiliary function to test
for it. Here's what my code looks like (note that I'm using Amazon Web Services):

Imports <xmlns="http://webservices.amazon.com/AWSECommerceService/2007-06-13">
....
....
....

Private Sub GetAmazonStuff()
'code to actually call Amazon Web Services omitted
Dim MyNodes = (From nd In doc...<Item_
Select New Item With {.ASIN = nd.<ASIN>.Value, _
.Image = nd.<SmallImage>.<URL>.Value, _
.Title = nd.<ItemAttributes>.<Title>.Value, _
.ListPrice = TestNode(nd.<ItemAttributes>.<ListPrice>), _
.OurPrice = TestNode(nd.<Offers>.<Offer>.<OfferListing>.<Price >)})
bindingSource.DataSource = MyNodes.ToList() 'error always occurs here & points to MyNodes
Me.dgvAmazon.DataSource = bindingSource
End Sub

Private Function TestNode(ByVal element As XElement) As String
If Not (element) Is Nothing Then
Return element.<FormattedPrice>.Value
Else
Return "no data"
End If
End Function
When running this code, I always get the "Object not set to an instance of an object"
error directed at the MyNodes variable. Code execution stops (as indicated above) despite
there being other nodes that haven't been evaluated. Debugging revealed that the error is
thrown whenever the TestNode function finds that the element Is Nothing, which means it
doesn't exist.

Why does that happen?

Thanks,
Roshawn
Jun 27 '08 #1
4 976
Roshawn wrote:
Dim MyNodes = (From nd In doc...<Item_
Select New Item With {.ASIN = nd.<ASIN>.Value, _
.Image = nd.<SmallImage>.<URL>.Value, _
.Title = nd.<ItemAttributes>.<Title>.Value, _
.ListPrice = TestNode(nd.<ItemAttributes>.<ListPrice>),
nd.<ItemAttributes>.<ListPrice>
returns IEnumerable(Of XElement) and not XElement which your function
TestNode expects.

_
.OurPrice = TestNode(nd.<Offers>.<Offer>.<OfferListing>.<Price >)})
Same here.
Private Function TestNode(ByVal element As XElement) As String
If Not (element) Is Nothing Then
Return element.<FormattedPrice>.Value
Else
Return "no data"
It is not clear to me what you want to achieve. Please post a relevant
sample of the XML your are trying to extract data from, then describe
which data you want to extract, then we can help with the LINQ.
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Jun 27 '08 #2
@Martin,

Thanks for the reply. Here is a sample of the xml I'm working with:

<ItemSearchResponse xmlns="http://webservices.amazon.com/AWSECommerceService/2005-10-05">
<OperationRequest>
<HTTPHeaders>
<Header Name="UserAgent" Value="IE">
</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>
</Item>
</Items>
</ItemSearchResponse>
As for nd.<ItemAttributes>.<ListPricereturning IEnumerable (Of XElement) instead of
XElement, Intellisense shows that it does return XElement. If it were wrong, then my
TestNode function would throw a conversion error or something, no. I get zero errors at
compile time. It occurs only when I'm working with the xml.

Thanks,
Roshawn
Jun 27 '08 #3
Roshawn wrote:
@Martin,

Thanks for the reply. Here is a sample of the xml I'm working with:

<ItemSearchResponse
xmlns="http://webservices.amazon.com/AWSECommerceService/2005-10-05">
<OperationRequest>
<HTTPHeaders>
<Header Name="UserAgent" Value="IE">
</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>
</Item>
</Items>
</ItemSearchResponse>

Here is a query that works with your sample:

Dim responseDoc As XDocument = XDocument.Load("..\..\XMLFile1.xml")
Dim query = _
From nd In responseDoc...<Item_
Let listPrice = nd.<ItemAttributes>.<ListPrice>(0) _
Let offers = nd.<Offers>(0) _
Select New With { _
.ASIN = nd.<ASIN>.Value, _
.Image = nd.<SmallImage>.<URL>.Value, _
.Title = nd.<ItemAttributes>.<Title>.Value, _
.ListPrice = TestPrice(listPrice), _
.OurPrice = TestOffers(offers)}

For Each item In query
Console.WriteLine("ASIN: {0}, Image: {1}, Title: {2},
ListPrice: {3}, OurPrice: {4}", item.ASIN, item.Image, item.Title,
item.ListPrice, item.OurPrice)
Next
The functions TestPrice and TestOffers look as follows:

Private Function TestPrice(ByVal element As XElement) As String
If element IsNot Nothing Then
Return element.<FormattedPrice>.Value
Else
Return "no data"
End If
End Function

Private Function TestOffers(ByVal element As XElement) As String
If element IsNot Nothing Then
Return
element.<Offer>.<OfferListing>.<Price>.<FormattedP rice>.Value
Else
Return "no data"
End If
End Function
End Module
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Jun 27 '08 #4
@Martin,

I appreciate the code you provided in your previous response. Didn't know much about the
"Let" parameter in Linq.

I ran this code and I received zero errors. Things work as expected. :)

Many thanks,
Roshawn
Jun 27 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: shapper | last post by:
Hello, I have been reading a few articles about LINQ and I have a few questions: 1. What do I need to start using it in my ASP.NET 2.0 / SQL 2005 / Visual Studio 2005 web sites? 2. Is...
3
by: plonk | last post by:
Hi I'm trying to learn sql by getting a database up and running on VB.net 2008 and SQL server express. I have the database connected correctly, (i think) but when i try to add a record using this...
22
by: paululvinius | last post by:
Hi! Testing som Linq-expressions and tried to measure performance and compare it to pre-Linq programming. The folloing two methods are functional equal but the non-Linq one is twice as fast....
0
by: ranganadh | last post by:
Dear Group members, I am new to LINQ, pls help on the deeling with huge amount of data with the C# stand Alone application. I have two file, which contains more then 2 lacs lines in every...
5
by: =?Utf-8?B?bWljaGFlbCBzb3JlbnM=?= | last post by:
After having played around with LINQ and reading the literature on it I have not been successful in getting this to work so I am looking for some guidance: I want to find a hunk in a collection...
5
by: Justin | last post by:
Here's my XML: <?xml version="1.0" ?> <AppMode Type="Network"> <CurrentFolder Path="c:\tabs"> <Tabs> <FilePath>tabs\Justin.tab</FilePath> <FilePath>tabs\Julie.tab</FilePath> *****There could...
2
by: Tom P. | last post by:
I'm not getting the theory behind LINQ, and without that I can't get my head around how to use it and why. Here's what I _think_ I'm trying to do: I've got a menu with several items. Some other...
6
by: shapper | last post by:
Hello, I want to compare two strings in a Linq Query. In this case "Car", "cAr", "CAR" would all be the same. Should I use ==, equals, ... ? What is the best way to do this?
4
by: CSharper | last post by:
I have following XML <root> <Person id="1"> <Name>a</Name> </Person> <Person id="2"> <Name>b</Name> </Person> </root>
3
by: =?Utf-8?B?UGF1bCBQcmV3ZXR0?= | last post by:
I'm attempting to use LINQ to insert a record into a child table and I'm receiving a "Specified cast is not valid" error that has something to do w/ the keys involved. The stack trace is: ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.