Read XML Help.

B

B-Dog

I'm trying to read two nodes in an xml file that I'm using to store setting
but I'm having a hard time trying to read the values of two nodes. Below I
can read one node but I need to be able to grab both values and I can't seem
to figure it out, just a bigginer. I'm able to grab aTime but can't get the
aType value. Should I create a second routine or is there something I can
stick in this to make it work for me. I just want those two value, the file
never changes just the values. Thanks

XML File

- <Sleep>
- <LastAlarm>
<aTime>5/28/2004 2:29:00 PM</aTime>
<aType>rdbLock</aType>
</LastAlarm>
</Sleep>
Code

Sub ReadXML()

Dim LastAlarm As String

Dim Doc As New XmlDocument

Dim Nav As XPath.XPathNavigator

Dim Iterator As XPath.XPathNodeIterator

'Load document.

Doc.Load(Application.StartupPath & "\" & "sleep.xml")

'Set nav object.

Nav = CType(Doc, XPath.IXPathNavigable).CreateNavigator()

'Set node iterator.

Iterator = Nav.Select("LastAlarm" & "aTime")

'Move to the desired node.

Iterator.MoveNext()

'Get the value of the current node.

LastAlarm = Iterator.Current.Value



End Sub
 
P

Patrick Steele [MVP]

I'm trying to read two nodes in an xml file that I'm using to store setting
but I'm having a hard time trying to read the values of two nodes. Below I
can read one node but I need to be able to grab both values and I can't seem
to figure it out, just a bigginer. I'm able to grab aTime but can't get the
aType value. Should I create a second routine or is there something I can
stick in this to make it work for me. I just want those two value, the file
never changes just the values. Thanks

An XPath query would probably be quicker:

http://www.w3schools.com/xpath/xpath_syntax.asp
 
G

Guest

dim aTime as string = doc.documentelement.selectsinglenode("//aTime").valu
dim aType as string = doc.documentelement.selectsinglenode("//aType").valu


----- B-Dog wrote: ----

I'm trying to read two nodes in an xml file that I'm using to store settin
but I'm having a hard time trying to read the values of two nodes. Below
can read one node but I need to be able to grab both values and I can't see
to figure it out, just a bigginer. I'm able to grab aTime but can't get th
aType value. Should I create a second routine or is there something I ca
stick in this to make it work for me. I just want those two value, the fil
never changes just the values. Thank

XML Fil

- <Sleep
- <LastAlarm><aTime>5/28/2004 2:29:00 PM</aTime><aType>rdbLock</aType></LastAlarm></Sleep
Cod

Sub ReadXML(

Dim LastAlarm As Strin

Dim Doc As New XmlDocumen

Dim Nav As XPath.XPathNavigato

Dim Iterator As XPath.XPathNodeIterato

'Load document

Doc.Load(Application.StartupPath & "\" & "sleep.xml"

'Set nav object

Nav = CType(Doc, XPath.IXPathNavigable).CreateNavigator(

'Set node iterator

Iterator = Nav.Select("LastAlarm" & "aTime"

'Move to the desired node

Iterator.MoveNext(

'Get the value of the current node

LastAlarm = Iterator.Current.Valu



End Su
 
B

B-Dog

That is what I needed tman, I'll try it

Thanks!

tMan said:
dim aTime as string = doc.documentelement.selectsinglenode("//aTime").value
dim aType as string = doc.documentelement.selectsinglenode("//aType").value


----- B-Dog wrote: -----

I'm trying to read two nodes in an xml file that I'm using to store setting
but I'm having a hard time trying to read the values of two nodes. Below I
can read one node but I need to be able to grab both values and I can't seem
to figure it out, just a bigginer. I'm able to grab aTime but can't get the
aType value. Should I create a second routine or is there something I can
stick in this to make it work for me. I just want those two value, the file
never changes just the values. Thanks

XML File

- <Sleep>
- <LastAlarm><aTime>5/28/2004 2:29:00
 
B

B-Dog

Hmnn, I tried this below but doesn't give me a value, only my error message
Try

Dim Doc As New XmlDocument

'Load document.

Doc.Load(Application.StartupPath & "\" & "sleep.xml")

Dim aTime As String = Doc.DocumentElement.SelectSingleNode("//aTime").Value

Dim aType As String = Doc.DocumentElement.SelectSingleNode("/aType").Value

MsgBox(aTime)

Catch ex As Exception

MsgBox("error")

End Try
 
G

Guest

oh ok..my bad!
instead of value use innertext property

Dim aTime As String = Doc.DocumentElement.SelectSingleNode("//aTime").InnerTex

Dim aType As String = Doc.DocumentElement.SelectSingleNode("/aType").InnerText
 

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