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
Dim strMyUrl As String
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.
="1.0"="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.
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.
="1.0"="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