Using XMLDocument - a newbie question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?RGF2aWRHQg==?=

    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
  • Martin Honnen

    #2
    Re: Using XMLDocument - a newbie question

    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.DocumentEle ment
    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 SelectSingleNod e with XPath 1.0
    expressions e.g.
    Dim foo As XmlNode = doc.SelectSingl eNode("//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.XmlD ocument, instead you use
    an XmlReader and call its various Read methods. With
    System.Xml.XmlD ocument 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:


    But ChildNodes/FirstChild/LastChild/NextSibling/PreviousSibling are the
    "navigation al" properties the W3C Core DOM defines, the power of .NET's
    DOM implementation is certainly its XPath 1.0 implementation with
    SelectNodes and SelectSingleNod e. 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:


    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

    Comment

    Working...