Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / October 2007

Tip: Looking for answers? Try searching our database.

How to read xml file nodes (vb.net)

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Elmo Watson - 23 Oct 2007 19:51 GMT
Scenario - I need to open up (dynamically) an xml file - in this case I'll
use the infamous 'books.xml':
- <catalog>
- <book id="bk101">
 <author>Gambardella, Matthew</author>
 <title>XML Developer's Guide</title>
 <genre>Computer</genre>
 <price>44.95</price>
 <publish_date>2000-10-01</publish_date>
 <description>An in-depth look at creating applications with
XML.</description>
 </book>

2 questions
I need to find out how to find what the base node is (in this case,
Catalog), and how to find what the other node is called (in this case
'book'), and how to get the attribute names (author, title, genre, etc) of
'book'

I don't need the data at this point - I just want to read the schema....

I've found tons of VB.Net xml code samples on the net, but just haven't been
able to find one that can do this particular thing.
Can anyone point me to a good tutorial, or show me how this is done?
Andrew Faust - 24 Oct 2007 02:25 GMT
You can use an XmlDocument object. Call the Load method to load up the
document then iterate through then ChildNodes collection and recursively
through each ChildNode's ChildNodes collection and grab the data you want.

Signature

Andrew Faust
andrew[at]andrewfaust.com
http://www.andrewfaust.com

> Scenario - I need to open up (dynamically) an xml file - in this case
> I'll use the infamous 'books.xml':
[quoted text clipped - 20 lines]
> been able to find one that can do this particular thing.
> Can anyone point me to a good tutorial, or show me how this is done?
Elmo Watson - 24 Oct 2007 19:26 GMT
Webpages/tutorials/samples for this?

> You can use an XmlDocument object. Call the Load method to load up the
> document then iterate through then ChildNodes collection and recursively
[quoted text clipped - 24 lines]
>> been able to find one that can do this particular thing.
>> Can anyone point me to a good tutorial, or show me how this is done?
Andrew Faust - 25 Oct 2007 04:37 GMT
Sorry, it's in c# as I don't use vb.net. The class libraries are all the
same, though, so it should be easy to get working in vb

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace xmlread
{
   class Program
   {
       static void Main(string[] args)
       {
           XmlDocument doc = new XmlDocument();
           doc.Load(args[0]);
           Console.WriteLine(doc.Name);
           OutputNodes(doc.ChildNodes, 1);
       }

       private static void OutputNodes(XmlNodeList nodes, int IndentLevel)
       {
           if (nodes == null) { return; }

           string s = new string('\t', IndentLevel);

           foreach (XmlNode node in nodes)
           {
               if (node.NodeType == XmlNodeType.Element)
               {
                   Console.WriteLine("{0}{1} = {2}", s, node.Name,
node.InnerText);
               }
               OutputNodes(node.ChildNodes, IndentLevel + 1);
           }
       }
   }
}

Signature

Andrew Faust
andrew[at]andrewfaust.com
http://www.andrewfaust.com

> Webpages/tutorials/samples for this?
>
[quoted text clipped - 28 lines]
>>> been able to find one that can do this particular thing.
>>> Can anyone point me to a good tutorial, or show me how this is done?
Elmo Watson - 24 Oct 2007 21:50 GMT
What i need to know is how to iterate through this xml file,
Find that Catalog is the root, then find Books as the recurring item, and
iterate the names of the Book Elements (not the data), to find the
following:
id
author
title
genre
price
publish date
description

> Scenario - I need to open up (dynamically) an xml file - in this case I'll
> use the infamous 'books.xml':
[quoted text clipped - 20 lines]
> been able to find one that can do this particular thing.
> Can anyone point me to a good tutorial, or show me how this is done?

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.