How to read xml file nodes (vb.net)

E

Elmo Watson

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?
 
A

Andrew Faust

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.
 
E

Elmo Watson

Webpages/tutorials/samples for this?


Andrew Faust said:
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.

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


Elmo Watson said:
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?
 
E

Elmo Watson

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
 
A

Andrew Faust

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);
}
}
}
}


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


Elmo Watson said:
Webpages/tutorials/samples for this?


Andrew Faust said:
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.

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


Elmo Watson said:
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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top