Help using XDocument in LINQ with ASP.Net (VB)

Hi,

Am having a problem using xDocument with LINQ. Searched on the internet for days but found nothing, think the problem is everyone is using VS and am hand coding so not sure what name spaces etc… to put and have found 3 different answers so far!

<%@ Page Language="VB" Debug="true" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Xml.Linq" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<script runat="server">

	Dim FeedXML As XDocument = XDocument.Load("http://www.fishoilbenefits.co.uk/omegaoils.xml")

	Dim Products = From Product In FeedXML.Descendants("Products") Select Name = Product.Element("p_name").Value

</script>

Am getting a error on the first XDocument saying it does not know what it is.

I have tried changing the namespace to .XLINQ but same error. Spoke to my host and they say they are running ASP 3, which I believe is the version that LINQ was introduced.

Any ideas ?

Dave

That would be .NET 3.5. ASP is not ASP.NET! ASP has nothing to do with what you are working on.
What is the actual text of the error? Is your site’s ASP.NET version set to 1.1.4322 or 2.0.50727 in IIS?

Yeah sorry I meant .Net not ASP.

It’s not my server, it’s hosted. I asked them about versions and they said they are running the latest.

Will upload it again and find the exact error message.

EDIT: The error am getting is -

Type ‘Xdocument’ is not defined.

Dave



   XDocument FeedXML = XDocument.Load("xml_file_goes_here");

        var query = from Product in FeedXML.Descendants("Products")
                    select new Product
                    {
                        Name = Product.Element("p_name").Value 
                       
                    };     


Not tested, however it should work.

I don’t know about VB but C# is case sensitive. Xdocument does not equal XDocument.

I don’t see that issue in the original example though.

No, VB.NET isn’t case sensitive. Anyways, my solution should work, unless their is more to his project.

My bad, my code is in C#. Can’t believe people are still using VB. :wink:

If I click ‘show detailed compiler output’ I have also noticed this error -

C:\Windows\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\root\f2a537c4\591c49da\App_Web_bjxfe8bc.0.vb(32) : warning BC40056: Namespace or type specified in the Imports ‘System.Xml.Linq’ doesn’t contain any public member or cannot be found. Make sure the namespace or the type is defined and contains at least one public member. Make sure the imported element name doesn’t use any aliases.

Imports System.Xml.Linq

Am I using the right Namespace ?

Dave

hmmm. That is a strange error. Yes, the namespace your importing is correct. PS. Since when was VB not case sensative?

I just tested your code in Visual Studio 2008 (in code behind on PageLoad) and it works fine. I had to make one change though, in your code you had the following:

FeedXML.Descendants(“Products”), while the XML has the following structure:


<catalogue>
 <product>
  <p_item_number>144</p_item_number>
  <p_category>Omega Oils</p_category>
  <p_name>Pure Superba Krill Oil 500mg 60 Soft Gels</p_name>

Changing Products to product gave me the expected result.

Dim FeedXML As XDocument = XDocument.Load("http://www.fishoilbenefits.co.uk/omegaoils.xml")
Dim Products = From Product In FeedXML.Descendants("product") Select Name = Product.Element("p_name").Value

May I ask why you are not using Visual Studio, with its debugging capabilities you can save yourself a lot of time finding these sorts of problems.

VB.NET isn’t case sensitive. However, VS will clean your code up for you by adding cases. But, if your hardcore and use a text-editor to build your websites,“Public Sub” isn’t different from “public sub”.

That is very interesting. lol You learn something new every day

Will change the Products to Product but still no idea why it isn’t working.

I don’t use Visual Studio as I don’t have a PC, I use a Mac (long story).

Dave

Can you beleive I still can not get this working.

Tried using VS 2008 and although the code runs, I can’t figure out how to access the fields in Products.

On this tutorial -

He does a For Next loop using Item and when he types Item. into VS it shows his field names. I can not get VS to do that at all, I just get a load of String type properties.

Dave

It’s sorted, posted on the asp.net forums and a guy sorted it.


Dim Products = From product In FeedXML.Descendants("product") Select New With { _  
  .ProdName = product.Element("p_name").Value _  
}  

Dave