469,645 Members | 1,634 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,645 developers. It's quick & easy.

Using XMLDocument - a newbie question

OK, so I've created and loaded an XMLDocument object.
But how do I go about using it? Specifically, how do I:
1) move to the first node (I assume I start on it when I load the XML?)
2) move to the next node (.read?)
3) move back to a previous node?

Is there a sample program somewhere to show this? (preferably VB.net)

TIA David
Aug 24 '08 #1
1 1538
DavidGB wrote:
OK, so I've created and loaded an XMLDocument object.
But how do I go about using it? Specifically, how do I:
1) move to the first node (I assume I start on it when I load the XML?)
2) move to the next node (.read?)
3) move back to a previous node?

Is there a sample program somewhere to show this? (preferably VB.net)
Assuming you have e.g.
Dim doc As New XmlDocument()
doc.Load("file.xml")
then the answers are as follows:
1) I am not sure why you want to "move" to nodes. Anyway, the first node
you have is the XmlDocument node itself. It has various properties and
methods to access its child and descendant nodes e.g.
Dim root As XmlDocument = doc.DocumentElement
gives you the document element (also called root element) that contains
any other elements.
doc.ChildNodes
is a collection to access all child nodes of the document. Then you can
apply the methods SelectNodes and SelectSingleNode with XPath 1.0
expressions e.g.
Dim foo As XmlNode = doc.SelectSingleNode("//foo")
looks for the first element named 'foo' at any level in the document.

2) If you want to process an XML document node by node in a forwards
only mode then you don't need a System.Xml.XmlDocument, instead you use
an XmlReader and call its various Read methods. With
System.Xml.XmlDocument you also have various properties to navigate, you
can access the FirstChild and NextSibling properties

3) If you have accessed a child node then accessing the ParentNode
property is the way back to the previous node. If you have accessed the
NextSibling property then PreviousSibling is the way back. See the
properties defined for XmlNode:
http://msdn.microsoft.com/en-us/libr...roperties.aspx

But ChildNodes/FirstChild/LastChild/NextSibling/PreviousSibling are the
"navigational" properties the W3C Core DOM defines, the power of .NET's
DOM implementation is certainly its XPath 1.0 implementation with
SelectNodes and SelectSingleNode. So learning XPath 1.0 certainly is a
good idea if you want to become a power user of .NET's DOM implementation.

Also note, if you use .NET 3.5 then there is LINQ to XML as a new object
model exploiting and leveraging the power of LINQ:
http://msdn.microsoft.com/en-us/library/bb387098.aspx

If you need more help then it might be best to look at a sample document
and access some nodes so consider to post a short sample and tell us
which nodes you want to access, then we can post some code.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Aug 25 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Naveed Anwar | last post: by
4 posts views Thread by Michel Verhagen | last post: by
5 posts views Thread by GaryDean | last post: by
2 posts views Thread by Jeff | last post: by
6 posts views Thread by =?Utf-8?B?VGFtbXkgTmVqYWRpYW4=?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.