Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP 3 Classic ASP Active Server Pages 3.0 > Classic ASP XML
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Classic ASP XML Using ASP 3 and XML. See also the XML category for more XML discussions not relating to ASP. NOT for ASP.NET 1.0, 1.1, or 2.0
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Classic ASP XML section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old July 28th, 2005, 11:04 AM
Banned
Points: 1,561, Level: 16
Points: 1,561, Level: 16 Points: 1,561, Level: 16 Points: 1,561, Level: 16
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2005
Location: , , .
Posts: 317
Thanks: 0
Thanked 0 Times in 0 Posts
Default Parse XML doc using VB.NET into ASP.NET page

I'm new to XML and .NET, and this is what I'm wanting to do: I want a simple way to parse an XML doc using VB.NET syntax into an ASP.NET page using the ASP.NET Web Matrix, not Visual Studio.NET. I then want to declare all of the elements and nodes into variables, and use that data within the webpage.

Note: I do not intend to use ASP.NET's built-in web controls; I intend to use regular HTML controls for this task.

From what I've read so far, there are several methods for parsing XML data using VB.NET, including XMLTextReader, XMLDocument, and XMLTextWriter. Since I only want to display the elements & child nodes from the XML doc, I'm assuming that I need to use the XMLTextReader method. So far, every example that I've found is very complex and involves code for Visual Studio.NET, which I don't use. So I'm looking for a very simple solution that doesn't contain a bunch of mumbo-jumbo code that doesn't need to be there. I'd greatly appreciate any help on this subject, and I hope to hear from someone soon. Thanks.

So here's a snippet from my source XML doc:
<ealerts>
    <alert>
        <title>eAlert - Title #1</title>
        <rss_url>#</rss_url>
        <link>#</link>
        <image_url>http://SERVER/DIRECTORY/images/gif/ealert_title1.gif</image_url>
        <width>49</width>
        <height>48</height>
        <status>active</status>
    </alert>
    <alert>
        <title>eAlert - Title #2</title>
        <rss_url>#</rss_url>
        <link>#</link>
        <image_url>http://SERVER/DIRECTORY/images/gif/ealert_title2.gif</image_url>
        <width>49</width>
        <height>48</height>
        <status>active</status>
    </alert>
</ealerts>

...and here's how I'd like to display these "eAlerts" depending on the XML doc's data:
<div id="topright">

        <a href="#"><img title="eAlert - Title #1" height="48" alt="eAlert - Title #1" src="images/gif/ealert_title1.gif" width="49" /></a>
        <a href="#"><img title="eAlert - Title #2" height="48" alt="eAlert - Title #2" src="images/gif/ealert_title2.gif" width="49" /></a>

</div>

KWilliams
Reply With Quote
  #2 (permalink)  
Old July 28th, 2005, 11:24 AM
Imar's Avatar
Wrox Author
Points: 71,164, Level: 100
Points: 71,164, Level: 100 Points: 71,164, Level: 100 Points: 71,164, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 16,955
Thanks: 79
Thanked 1,556 Times in 1,533 Posts
Default

Hi there,

Is there any reason you don't want to use the built-in server controls? It feels a bit odd to use a technology, but not use its most important concepts. It's like buying a fast new car, but never switch into the second gear.

With those controls, things can be as easy as this:

First, add a DataGrid to your ASPX page:

  <asp:DataGrid ID="Grid1" Runat="server" />

then in the Code Behind (or in the page itself) in Page_Load add something like this:
Code:
Imports System.Data ' This goes at the top of the page, or with an @Import directive

Dim ds As DataSet = New DataSet
ds.ReadXml(Server.MapPath("XMLFile1.xml"))

Grid1.DataSource = ds
Grid1.DataBind()
When you run this page, you get a plain grid with the records from the XML file.

You can customize the DataGrid with hyper link controls or whatever you see fit.

IMO, not choosing server controls is a big mistake, but maybe you have a good reason to do so??

Cheers,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Reply With Quote
  #3 (permalink)  
Old July 28th, 2005, 11:39 AM
Banned
Points: 1,561, Level: 16
Points: 1,561, Level: 16 Points: 1,561, Level: 16 Points: 1,561, Level: 16
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2005
Location: , , .
Posts: 317
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Imar,

Thanks for the quick reply. To answer your question about why I don't want to use the ASP.NET built-in web controls, that's because I had nothing but trouble in figuring out how to use XML data within those controls for some time now. The biggest issue concerned how to access not only the main element from an XML file, but also all of its child nodes. Everytime I tried to use the method that you referred to, I'd get error messages whenever an XML doc contained more then one tier of child nodes. All I wanted to do was to pull all of the properties for each element into a set of variables, and then display that data. But unfortunately everytime I tried to get some advice on that subject from different forums, no one would reply.

Another BIG issue with the built-in web controls was their efficiency. Each page took forever to load, when regular VB.NET script referenced in a regular HTML control did not. Even if I was able to get the "Dataset" setup to work, this would always be a problem, as I want for my site to reasonably fast for almost all users.

So after a lot of frustration without success, I decided that it would likely be best to use the XMLTextReader and XMLDocument methods through a VB.NET script, and then apply those variables to different sections of the page without the use of web controls.

If you know of a solution to my problems with the ASP.NET web control's efficiency, problems with pulling in XML data, please let me know. And maybe then I'd reconsider my current position. Thanks again for your response, as it's greatly appreciated.

KWilliams
Reply With Quote
  #4 (permalink)  
Old July 28th, 2005, 12:15 PM
Imar's Avatar
Wrox Author
Points: 71,164, Level: 100
Points: 71,164, Level: 100 Points: 71,164, Level: 100 Points: 71,164, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 16,955
Thanks: 79
Thanked 1,556 Times in 1,533 Posts
Default

If you want to read an XML document line by line, you may want to look in to the XPathNavigator class: http://msdn.microsoft.com/library/de...ClassTopic.asp

This allows you to walk a document, selecting elements and attributes that match your criteria.

But did you try out my code? Does that work for you? It runs just as fast as any other page here. With a few nested controls (e.g HyperLink controls) you can quickly establish what you need....

Cheers,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Reply With Quote
  #5 (permalink)  
Old July 28th, 2005, 12:21 PM
Banned
Points: 1,561, Level: 16
Points: 1,561, Level: 16 Points: 1,561, Level: 16 Points: 1,561, Level: 16
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2005
Location: , , .
Posts: 317
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I did try your code, but it gave me the same error as I always get. It doesn't recognize any child nodes from the source XML file; only the first tier element. So that can't help me if I have an XML doc with more than one tier of nodes.

I'll check out your referenced article and other info on the XPathNavigator class, and hopefully that will be what I need. But I'm finding this entire process of simply trying to access an XML document and display all of that doc's nodes to be rather daunting. I thought that the whole process would be simpler than this. But thanks again for the info.

KWilliams
Reply With Quote
  #6 (permalink)  
Old July 28th, 2005, 12:32 PM
Imar's Avatar
Wrox Author
Points: 71,164, Level: 100
Points: 71,164, Level: 100 Points: 71,164, Level: 100 Points: 71,164, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 16,955
Thanks: 79
Thanked 1,556 Times in 1,533 Posts
Default

What error do you get exactly? Can you post it here?

Instead of this:

Grid1.DataSource = ds

try this:

Grid1.DataSource = ds.Tables(0).DefaultView

That should work and show you a two dimensional table with the XML you posted here. I get two records each with 7 columns. Isn't that what you're trying to accomplish??

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Reply With Quote
  #7 (permalink)  
Old July 28th, 2005, 12:56 PM
Banned
Points: 1,561, Level: 16
Points: 1,561, Level: 16 Points: 1,561, Level: 16 Points: 1,561, Level: 16
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2005
Location: , , .
Posts: 317
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The example XML doc that I posted intentionally only contained 1 tier of data because I wanted to keep it simple for my original request. But if I try to use that method for an XML doc like this:
<ROOT>
 <ELEMENT>
  <CHILDNODE1>...
   <CHILDNODE2>...</CHILDNODE2>
  </CHILDNODE1>
 </ELEMENT>
</ROOT>

The "ROOT", "ELEMENT", and "CHILDNODE1" data will display fine. But attempting to place "CHILDNODE2" inslide of a web control results in this error message:
Server Error in '/' Application.
CHILDNODE2 is neither a DataColumn nor a DataRelation for table ROOT.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentException: CHILDNODE2 is neither a DataColumn nor a DataRelation for table ROOT.

In the meantime, I'll try your suggestion of trying this:
Grid1.DataSource = ds.Tables(0).DefaultView

...instead of this:
Grid1.DataSource = ds

KWilliams
Reply With Quote
  #8 (permalink)  
Old July 28th, 2005, 01:39 PM
Imar's Avatar
Wrox Author
Points: 71,164, Level: 100
Points: 71,164, Level: 100 Points: 71,164, Level: 100 Points: 71,164, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 16,955
Thanks: 79
Thanked 1,556 Times in 1,533 Posts
Default

Quote:
quote:Originally posted by kwilliams
The example XML doc that I posted intentionally only contained 1 tier of data because I wanted to keep it simple for my original request.
Right, that isn't really helpful and caused a lot of confusion ;)

Anyway, you say you have "code for Visual Basic .NET" and you don't use that. However, the same code should run fine in Web Matrix or even in pages created in Notepad. Would it help if you posted that code here?

Otherwise, I think you need to load the document into an XmlDocument and then use its properties like ChildNodes or FirstChild to get the nodes you do want to work with. Take a look here: http://msdn.microsoft.com/library/de...mberstopic.asp

I don't use Xml in these kind of scenarios much, so I don't have any ready made examples here. I am sure that Google should bring up loads of useful stuff though...

Cheers,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Reply With Quote
  #9 (permalink)  
Old July 28th, 2005, 02:21 PM
Banned
Points: 1,561, Level: 16
Points: 1,561, Level: 16 Points: 1,561, Level: 16 Points: 1,561, Level: 16
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2005
Location: , , .
Posts: 317
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by kwilliams
The example XML doc that I posted intentionally only contained 1 tier of data because I wanted to keep it simple for my original request.
Quote:
quote:Reply posted by ImarRight, that isn't really helpful and caused a lot of confusion ;)
What I was meaning is that I only wanted to get the code that would use one of the XML parse methods to extract data from an XML file. My original post wasn't concerning how to use ASP.NET's web controls, because of the problems I ran into in the past.

Quote:
quote:Anyway, you say you have "code for Visual Basic .NET" and you don't use that. However, the same code should run fine in Web Matrix or even in pages created in Notepad.
That was what I was originally asking for...VB.NET code that uses the XMLTextReader or XMLDocument parsing method to pull in XML data, and then assign that data to variables. My problem with the referenced sites (like http://msdn.microsoft.com/library/de...ctortopic1.asp) is that they are intended for developers who use VisualStudio.NET and all of its built-in functions. I simply wanted to see if there was a way to accomplish this task in a much simpler fashion, without all of the properties meant for VisualStudio.NET developers.

Quote:
quote:Would it help if you posted that code here?
Sure.

Quote:
quote:Otherwise, I think you need to load the document into an XmlDocument and then use its properties like ChildNodes or FirstChild to get the nodes you do want to work with. Take a look here: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemxmlxmldocumentmemberstopic.asp
This is basically what I'm looking for, without all of the extra stuff meant for a VisualStudio.NET environment.

It's really too bad that almost all examples using VB.NET assume that you use VisualStudio.NET. Personally, I don't need VisualStudio.NET, as it's like a hammer when I need a needle. I'm not exactly sure why Microsoft developed the Web Matrix when they don't give enough examples of how to do complex tasks within their own program. But I guess...that's Microsoft.

Quote:
quote:I don't use Xml in these kind of scenarios much, so I don't have any ready made examples here. I am sure that Google should bring up loads of useful stuff though...
Well, I've appreciated your quick and thorough advice, and I'll make sure to check Google's resources out also. Thanks again Imar:)

KWilliams
Reply With Quote
  #10 (permalink)  
Old July 28th, 2005, 02:32 PM
Imar's Avatar
Wrox Author
Points: 71,164, Level: 100
Points: 71,164, Level: 100 Points: 71,164, Level: 100 Points: 71,164, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 16,955
Thanks: 79
Thanked 1,556 Times in 1,533 Posts
Default

I am not sure I understand the problem with code for Visual Studio. What's there in this page: http://msdn.microsoft.com/library/de...ctortopic1.asp that requires Visual Studio??

All you need to do is move the Imports statement to an Import directive, and then you can use all of the code directly. Obviously, Console.WriteLine is for a console app so you need something like Response.Write or, better, myLabel.Text = but other than that, that code works fine in the Web Matrix.

But maybe I am overlooking something??

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to Use JavaScript in an ASP.NET/VB page kwilliams ASP.NET 2.0 Professional 5 January 14th, 2008 03:30 PM
parse error xml load document asp.net academics2006 ASP.NET 1.0 and 1.1 Basics 0 March 13th, 2006 03:21 PM
Newbie: Calling a VB doc from ASP.NET kwilliams Classic ASP Basics 2 August 31st, 2005 10:10 AM
How can I call a vb.net Exe from an asp.net page? jorevil Classic ASP Basics 0 October 1st, 2003 12:34 PM



All times are GMT -4. The time now is 12:45 AM.


Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.