Read contents of XML element

G

Guest

I have an XML document that looks like

<request>
<login>
<username>myuser</username>
<password>mypass</password>
</login>
</request>

How do I get the values of username and password into a string?

I know how to do this if there are multiple <login></login> parts:
im Files As New ArrayList
Dim nodelist As XmlNodeList
Dim node As XmlNode

nodelist = xmldoc.SelectNodes("/REQUEST/IMPORT/DOCUMENT/FIELD")
For Each node In nodelist
If node.Attributes.GetNamedItem("CODE").Value = "BASE64" Then
Dim Filename As String = node.Item("DATA").InnerText()
If File.Exists(Filename) Then
Files.Add(Filename)
End If
End If
Next

When I try:

Dim nodelist As XmlNodeList
Dim node As XmlNode
node = xmldoc.SelectSingleNode("/REQUEST/LOGIN")


Dim login() As String
nodelist = xmldoc.SelectNodes("/REQUEST/LOGIN")

login(0) = node.Item("USERNAME").Value
login(1) = node.Item("PASSWORD").Value

node.item("USERNAME").value and node.item("PASSWORD").value are nothig and
I get a null reference error.
 
G

Guest

Sorry, everything should be in UPPERCASE, so it is not uppercase, lowercase
problem
 
J

JohnFol

Couple of ideas for you . . ..



Dim xmldoc As New Xml.XmlDocument

xmldoc.LoadXml("<REQUEST><LOGIN><USERNAME>myuser</USERNAME><PASSWORD>mypass</PASSWORD></LOGIN></REQUEST>")

Dim nodelist As XmlNodeList

Dim node As XmlNode

node = xmldoc.SelectSingleNode("/REQUEST/LOGIN")

MsgBox(xmldoc.SelectSingleNode("//USERNAME").InnerText)

MsgBox(xmldoc.SelectSingleNode("/REQUEST/LOGIN/PASSWORD").InnerText)

nodelist = node.SelectNodes("/REQUEST/LOGIN")

MsgBox(nodelist(0).SelectSingleNode("USERNAME").InnerText)

MsgBox(node.SelectSingleNode("PASSWORD").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