Connecting Tech Pros Worldwide Forums | Help | Site Map

XML Node Wildcard?

Terry Olsen
Guest
 
Posts: n/a
#1: Nov 21 '05
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.SelectSingleNode("/joblog/backup/set/directory/directory/director
y/drive_error").InnerText
MsgBox(DriveError)
End Sub

However, the "drive_error" 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
Guest
 
Posts: n/a
#2: Nov 21 '05

re: XML Node Wildcard?



"Terry Olsen" <tolsen64@hotmail.com> wrote in message
news:eESFip9WFHA.3996@TK2MSFTNGP09.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.SelectSingleNode(
: "/joblog/backup/set/directory/directory/director
: y/drive_error").InnerText
: MsgBox(DriveError)
: End Sub
:
: However, the "drive_error" 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>SomeValue</drive_error>
</directory>
</directory>
</directory>
</set>
<set>
<directory>
<directory>


<!-- 2 directories deep -->
<drive_error>SomeOtherValue</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(xPath)
Console.WriteLine(node.innerText)
Next

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

Ralf


Closed Thread


Similar Visual Basic .NET bytes