Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need a simple code example of how to read through the following XML using XMLReader in VB.net. I have been able to read the topxml section and the DETAIL section but have been unable to get the SUBDETAIL section items. The sample XML source below is returned by a WEB service.

I am trying to read the following from the below XML, ID, TITLE, and GROUPID from
<SUBDETAIL ID="A1B2" TITLE="XYZ Title" GROUPID="2"/> 


WEB Service Returned XML:
 <?xml version="1.0" encoding="UTF-8" ?>
<topxml status="ok" time="63">
   <DETAIL ID="123457" VAL1="ABC" VAL2="DEF">
     <SUBDETAIL ID="A1B2" TITLE="XYZ Title" GROUPID="2"/>
   </ DETAIL>
 </ topxml>

Thanks

ADDED 1/29/2014
Thanks Peter..
This works when using your example with hard coded XML for the xmlString variable and Using reader As XmlReader = XmlReader.Create(New StringReader(xmlString)). However when reading the XML returned from the URL/WEB service the reader.Value is Nothing/Empty.

UPDATED 1/29/2014
It appears that when using XMLReader that the returned XML is read in pieces (i.e. reader.read ) and that when I loop through the XML in VB.net the returned sections are as follows.

topxml
DETAIL


The SUBDETAIL part of the XML appears to be missing from the XMLReader or I am not using the correct call to get to the CHILD level of the DETAIL.

VB.net Code snippet
VB
Dim strMyUrl As String
' url
strMyUrl = "http://www.somplace.com/subfold/xyz/GetInfo.cfm?LOGInUName=JOHN&LINPassword=mypass&username=123457"
Dim reader As XmlReader = XmlReader.Create(strMyUrl)
    reader.ReadToFollowing("SUBDETAIL")
    reader.MoveToFirstAttribute()
    Dim id As String = reader.Value
    Debug.Print("SUBDETAIL ID: " + id)

XML returned from the URL/WEB Service:

Sorry for any confusion but the XML is as follows and the CASE was incorrectly pasted from the orginal question.. Here is the XML from the original post as it comes from the WEB service. The below XML is of the format that is returned in the browser when calling the WEB service URL.
However when the URL is used in my vb.net code the returned XML does not appear to have the SUBDETAIL section, or I am not using the correct calls thus the root of my POST, needing to be able to read the SUBDETAIL tems attributes, ID, TITLE, GROUPID.
XML
<?xml version="1.0" encoding="UTF-8"?>
<topxml status="ok" time="63">
   <DETAIL ID="123457" VAL1="ABC" VAL2="DEF">
     <SUBDETAIL ID="A1B2" TITLE="XYZ Title" GROUPID="2"/>
   </ DETAIL>
 </ topxml>


UPDATED 1/30/2014

I have tried to use the following and I am still not getting the data.
VB
reader.ReadToFollowing("DETAIL")
reader.ReadToDescendant("SUBDETAIL")

Any ideas that are simpler to read the required values from the XML other than XMLReader would be greatly appreciated but would prefer the ability to use my existing code with XMLReader. Thanks!
NOTE: I am able to read ALL attributes from the DETAIL section, ID1, FNAME, LNAME, EMAIL.

I need to be able to read the SUBDETAIL values ID2, TITLE, GROUPID, ORGCODE

Here is the XML that I am receivig from the WEB service.
XML
<?xml version="1.0" encoding="UTF-8" ?>
    <topxml status="OK" time="34">
        <DETAIL ID1="1234567" FNAME="John" LNAME="Doe" EMAIL="JDoe@Company.com">
            <SUBDETAIL ID2="A123-4567" TITLE="A Title Here" GROUPID="1" ORGCODE="ABC" />
        </DETAIL>
    </topxml>

Thanks
Ed
Posted
Updated 30-Jan-14 4:14am
v4

Quote:
Any ideas that are simpler to read the required values from the XML other than XMLReader would be greatly appreciated
Personally, I find this approach easier.
XML
Private Sub TestXML()

 Dim sbSource As New System.Text.StringBuilder(200)
 With sbSource
   .AppendLine("<?xml version=""1.0"" encoding=""UTF-8""?>")
   .AppendLine("<topxml status=""OK"" time=""34"">")
   .AppendLine("<DETAIL ID1=""1234567"" FNAME=""John"" LNAME=""Doe"" EMAIL=""JDoe@Company.com"">")
   .AppendLine("    <SUBDETAIL ID2=""A123-4567"" TITLE=""A Title Here"" GROUPID=""1"" ORGCODE=""ABC""/>")
   .AppendLine("</DETAIL>")
   .AppendLine("<DETAIL ID1=""1234568"" FNAME=""Jane"" LNAME=""Doe"" EMAIL=""JnDoe@Company.com"">")
   .AppendLine("    <SUBDETAIL ID2=""A123-4562"" TITLE=""A Title Here"" GROUPID=""1"" ORGCODE=""ABC""/>")
   .AppendLine("</DETAIL>")
   .AppendLine("</topxml>")
 End With

Dim strMyUrl As String = "http://www.somplace.com/subfold/xyz/GetInfo.cfm?LOGInUName=JOHN&LINPassword=mypass&"

Dim doc As New XmlDocument
doc.LoadXml(sbSource.ToString()) ' delete this
'doc.Load(strMyUrl)              ' and use this line to load from url

For Each detail As XmlElement In doc.DocumentElement.GetElementsByTagName("DETAIL")
   For Each subdetail As XmlElement In detail.GetElementsByTagName("SUBDETAIL")
      Dim FName As String = GetAttibuteValue(detail, "FNAME")
      Dim LName As String = GetAttibuteValue(detail, "LNAME")
      Dim id2 As String = GetAttibuteValue(subdetail, "ID2")
      Dim title As String = GetAttibuteValue(subdetail, "TITLE")
      Dim groupid As String = GetAttibuteValue(subdetail, "GROUPID")
      Dim orgcode As String = GetAttibuteValue(subdetail, "ORGCODE")
   Next subdetail
Next detail
End Sub ' Test

Private Function GetAttibuteValue(ByVal node As XmlNode, ByVal attibutename As String) As String
   Dim ret As String = String.Empty
   If node IsNot Nothing AndAlso node.Attributes IsNot Nothing Then
      Dim attrib As XmlNode = node.Attributes.GetNamedItem(attibutename)
      If attrib IsNot Nothing Then
         ret = attrib.Value
      End If
   End If
   Return ret
End Function 'GetAttibuteValue
 
Share this answer
 
v2
Adapt from my sample code below:
VB
Imports System.Xml
Imports System.IO
Imports System.Text

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim xmlString As String = "<topxml status='ok' time='63'>" & _
        "<DETAIL ID='123457' VAL1='ABC' VAL2='DEF'>" & _
                        "<SUBDETAIL ID='A1B2' TITLE='XYZ Title' GROUPID='2'/> " & _
        "</ DETAIL>" & _
        "</ topxml>"

        ' Create an XmlReader
        Using reader As XmlReader = XmlReader.Create(New StringReader(xmlString))

            reader.ReadToFollowing("SUBDETAIL")
            reader.MoveToFirstAttribute()
            Dim id As String = reader.Value
            TextBox1.Text = "SUBDETAIL ID: " + id

            reader.MoveToNextAttribute()
            Dim title As String = reader.Value
            TextBox2.Text = "SUBDETAIL TITLE: " + title


        End Using

    End Sub
End Class
 
Share this answer
 
v4
Comments
TheITManager 29-Jan-14 9:47am    
This works when using your example with hard coded XML for the xmlString variable and Using reader As XmlReader = XmlReader.Create(New StringReader(xmlString)). However when reading the XML returned from the URL/WEB service the reader.Value is Nothing/Empty. Dim strMyUrl As String 'url returns XML strMyUrl = "http://www.somplace.com/subfold/xyz/GetInfo.cfm?linUName=JOHN&LINPassword=mypass&username=123457" Dim reader As XmlReader = XmlReader.Create(strMyUrl) reader.ReadToFollowing("SUBDETAIL") reader.MoveToFirstAttribute() Dim id As String = reader.Value Debug.Print("SUBDETAIL ID: " + id) The WEB Service returns the following example XML: - -


See my UPDATES in the original post.. Thanks Ed
Try:
VB
reader.ReadToFollowing("DETAIL")
reader.ReadToDescendant("SUBDETAIL")
 
Share this answer
 
Comments
TheITManager 30-Jan-14 10:15am    
I have tried to use the code previously as you suggest and I am not getting the data.
See my UPDATES 1/30/2014. Thanks!
Your later attempt shows the problem.
Remember that XML is case-sensitive!
VB
reader.ReadToFollowing("SUBDETAIL")

will not find:
XML
<subdetail id="A1B2" title="XYZ Title" groupid="2" />
 
Share this answer
 
Comments
TheITManager 29-Jan-14 9:50am    
The CASE was correct in the original post and then pasted wrong. Sorry for any confusion. See my updates to the question. Still having problems getting to the SUBDETAIL section.
here's my working example, though albeit near to your request, but this might give you hint how to extract data from the node up to the last one...

Dim xmlDoc As New System.Xml.XmlDocument

xmlDoc.Load("d:\product.xml")
Dim xmlNode As System.Xml.XmlNodeList = xmlDoc.GetElementsByTagName("product")
Dim xmlChild As System.Xml.XmlNode, _
    str As String = ""
For i As Integer = 0 To xmlNode.Count - 1
    str &= EnumChild(xmlNode(i))
Next
MsgBox(str)


    Function EnumChild(ByVal _xmlNode As System.Xml.XmlNode) As String
        Dim retval As String = ""

        Select Case _xmlNode.NodeType
            Case Xml.XmlNodeType.Element
                Dim _xml As System.Xml.XmlElement = _xmlNode
                If _xml.InnerText = _xml.InnerXml Then
                    retval &= _xml.Name & ": " & _xml.InnerText & vbCrLf
                Else
                    retval &= _xml.Name & vbCrLf
                End If
        End Select

        For Each _xml As System.Xml.XmlNode In _xmlNode.ChildNodes
            retval &= EnumChild(_xml)
        Next

        Return retval
    End Function
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900