XML Node Wildcard?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Terry Olsen

    #1

    XML Node Wildcard?

    I'm using the following to pull a string from an XML Log file:

    Public Sub ReadXMLFile()
    Dim xmlDoc As New XmlDocument
    xmlDoc.Load("d: \test.xml")
    Dim DriveError As String =
    xmlDoc.SelectSi ngleNode("/joblog/backup/set/directory/directory/director
    y/drive_error").I nnerText
    MsgBox(DriveErr or)
    End Sub

    However, the "drive_erro r" part of the node could be any number of
    "directory" 's deep depending on where the error occurred during the
    backup.

    Is there a wildcard or some other way I could grab the drive_error text
    regardless of how deep it is?

    Thanks.

    *** Sent via Developersdex http://www.developersdex.com ***
  • _AnonCoward

    #2
    Re: XML Node Wildcard?


    "Terry Olsen" <tolsen64@hotma il.com> wrote in message
    news:eESFip9WFH A.3996@TK2MSFTN GP09.phx.gbl...
    :
    : I'm using the following to pull a string from an XML Log file:
    :
    : Public Sub ReadXMLFile()
    : Dim xmlDoc As New XmlDocument
    : xmlDoc.Load("d: \test.xml")
    : Dim DriveError As String =
    : xmlDoc.SelectSi ngleNode(
    : "/joblog/backup/set/directory/directory/director
    : y/drive_error").I nnerText
    : MsgBox(DriveErr or)
    : End Sub
    :
    : However, the "drive_erro r" part of the node could be any number of
    : "directory" 's deep depending on where the error occurred during the
    : backup.
    :
    : Is there a wildcard or some other way I could grab the drive_error
    : text regardless of how deep it is?
    :
    : Thanks.
    :
    : *** Sent via Developersdex http://www.developersdex.com ***


    Yes: "/joblog/backup/set/*//drive_error"


    Assume your xml document looks like this:
    ---------------------------------------------------
    <joblog>
    <backup>
    <set>
    <directory>
    <directory>
    <directory>

    <!-- 3 directories deep -->
    <drive_error>So meValue</drive_error>
    </directory>
    </directory>
    </directory>
    </set>
    <set>
    <directory>
    <directory>


    <!-- 2 directories deep -->
    <drive_error>So meOtherValue</drive_error>
    </directory>
    </directory>
    </set>
    </backup>
    </joblog>
    ---------------------------------------------------


    This code will access both drive_error nodes even though they aren't
    nested as deeply.

    ---------------------------------------------------
    Option Strict

    Imports System
    Imports System.XML

    Public Class [Class]
    Public Shared sub Main()
    Dim x As New XMLDocument
    Dim node As XMLNode


    Const xPath As String = _
    "/joblog/backup/set/*//drive_error"

    x.Load("tmp.xml ")

    For Each node In x.selectNodes(x Path)
    Console.WriteLi ne(node.innerTe xt)
    Next

    End Sub
    End Class
    ---------------------------------------------------

    Ralf


    Comment

    Working...