Store XML Tag into Array

digitaldrew

Well-known member
Joined
Nov 10, 2012
Messages
167
Programming Experience
Beginner
Hello everyone, I've ran into a bit of an issue and some help would be appriciated.

I'm receiving a response back in XML and I would like to store one of the tags into an array. The response looks like this:

VB.NET:
<response clTRID="ee9c5514-e169-4735-8793">
 <result code="1004">
  <msg>messages waiting</msg>
 </result>
 <msgQ count="2" date="03-04-2013 22:02:11" />
 <resdata>
 <REPORT><ITM order="152131" roid="1" riid="1" resourceid="domain:29576" status="2" timestamp="3/4/2013 10:02:11 PM" /><ITEM order="152131" roid="1" riid="2" resourceid="domain:28664" status="2" timestamp="3/4/2013 10:02:11 PM"/></REPORT>
 </resdata>
</response>

The tag I'm looking for specifically is the "resourceid"..There were two orders submitted during this request and so it's returned two resourceid's. Any help on getting xmlreader to find each of these tags and store them into an array would be very helpful!!

Thanks in advance
 
Hi,

If you have your Response in an XML Document then you can use the GetElementsByTagName to get each ITEM and then get the ResourseID attribute using GetAttribute. Have a look at this example:-

VB.NET:
Imports System.Xml
Imports System.IO
 
Public Class Form1
  Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    'Ignore this load information - just getting your data from a file
    Dim myReader As New StreamReader(Application.StartupPath & "\XMLFile1.xml")
    Dim myXMlReader As New XmlTextReader(myReader)
    Dim myXMLDoc As New XmlDocument()
    myXMLDoc.Load(myXMlReader)
 
    'I have used two For Loops here to accommodate for the spelling
    'of ITEM or ITM but hoepfully you should be able to get rid of
    'one loop when you have confirmed the spelling of the Tag name
    For Each myElements As XmlElement In myXMLDoc.GetElementsByTagName("ITEM")
      Dim myString = myElements.GetAttribute("resourceid")
      MsgBox(myString)
    Next
    For Each myElements As XmlElement In myXMLDoc.GetElementsByTagName("ITM")
      Dim myString = myElements.GetAttribute("resourceid")
      MsgBox(myString)
    Next
  End Sub
End Class

The one things that seems strange is the spelling of ITEM in your Response. You currently have ITEM and ITM? To accommodate this I have done two loops above but if you recognise this to be an error then you can get rid of one of the loops.

Hope that helps.

Cheers,

Ian
 
Dim ids = From item In XDocument.Parse(xmlstring)...<ITEM> Select item.@resourceid
 
Back
Top