Click here to Skip to main content
15,388,688 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
XML
I am trying to read data from an xml file (API) using the repeater control in vb.net. but I am getting the following error: XmlNodeList' is a type and cannot be used as an expression. 

The error is pointing at this line from the code below:

XmlNodeList nodes = doc.SelectNodes("products/product")
Can anyone please help fix this error?

The code is shown below:

<Script runat="server">
sub Page_Load
if Not Page.IsPostBack then Dim doc  as XmlDocument = new XmlDocument()
doc.Load("this is the url link")
 XmlNodeList  nodes  = doc.SelectNodes("products/product")
rpMyRepeater.DataSource = nodes
rpMyRepeater.DataBind()
end if
end sub
</script>

<html>
<body>
<asp:repeater id="rpMyRepeater" runat="server">
<HeaderTemplate>>
</HeaderTemplate>
   <ItemTemplate>

<%# ((System.Xml.XmlNode)Container.DataItem).Attributes["description"].Value %>
 <%# ((System.Xml.XmlNode)Container.DataItem).InnerXml %>

   </ItemTemplate>
   <FooterTemplate>
   </FooterTemplate>
</asp:repeater>
Posted
Comments
Sergey Alexandrovich Kryukov 24-Aug-15 14:56pm
   
In what line? Isn't it in doc.Load("this is the url link")?
—SA
Afzaal Ahmad Zeeshan 24-Aug-15 15:07pm
   
Plus, please note the case sensitivity in VB.NET! if is not the If keyword.
Richard Deeming 24-Aug-15 15:17pm
   
VB is case insensitive. :)
Afzaal Ahmad Zeeshan 24-Aug-15 15:29pm
   
Ah, my bad.

Chances are, what you called "this is the url link" is not a valid XML document, most likely, it is not well-formed. You can load it in any Web browser; it will show you where the bug in the file is.

—SA
   
You're mixing VB and C# syntax. In VB, the declaration needs to be:
VB.NET
Dim nodes As XmlNodeList = doc.SelectNodes("products/product")
   
Comments
hm9 24-Aug-15 15:44pm
   
Ok thanks I corrected the code as you suggested and the error is cleared but it is complaining about this line:

<![CDATA[<%# ((System.Xml.XmlNode)Container.DataItem).Attributes["description"].Value %>]]>

The error:
'XmlNode' is a type in 'Xml' and cannot be used as an expression.

any ideas? is it syntax issue?
Richard Deeming 24-Aug-15 15:46pm
   
You're still using C# syntax.

<![CDATA[<%# DirectCast(Container.DataItem, System.Xml.XmlNode).Attributes("description").Value %>]]>
hm9 24-Aug-15 16:16pm
   
Ok I corrected the syntax now. Regarding the xml, below is an extract of the xml file, I am trying to access. The nodes I am trying to get to is products/product and the value of ns2:description is what i want to output. but it doesnt show anything on the page. I did change the 'description' for 'ns2:description' but it didnt make a difference

<result xmlns="urn:com:trade:pf:model:xml:output" xmlns:ns2="urn:com:trade:pf:model:xml:common" version="3.0">
<productHeader>
<totalhits>591727
</productHeader>
<products>
<product language="en">
<ns2:name>La Roche Posay
<ns2:description>
La Roche Posay - Effaclar H etc
Richard Deeming 24-Aug-15 16:22pm
   
Sounds like you have a new question. You'll need to post the full XML file, including the namespace declarations. Make sure you properly encode the XML when you post it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900