XML Node Wildcard?

T

Terry Olsen

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

_AnonCoward

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


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
 

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