Reading an XML file using XmlDocument

Currymunch

Member
Joined
Jan 21, 2008
Messages
13
Programming Experience
Beginner
Hi all, I'm having a spot of bother when it comes to reading a file from XML into vb.net 2003.

using a previous poster's help thus far i have wrangled up


'**************************************************
'responds to a click event

VB.NET:
        Dim doc As New XmlDocument
        doc.Load("XmlTest.xml")
        Dim xmlNlist As XmlNodeList = doc.SelectNodes("//spy")
        If ((Not xmlNlist Is Nothing) AndAlso (xmlNlist.Count > 0)) Then

            Dim xnId As XmlNode

            For Each xnId In xmlNlist
                spyList.Items.Add(xnId.InnerText)
            Next
        End If

    End Sub

'*****************************************

Whilst this is a step forward for my project I am looking to delimit the String that you get, thus

HTML:
<spyList>
	<spy>
		<firstName>Jo</firstName>
		<surname>Bloggs</surname>
		<codeName>JB</codeName>
		<age>23</age>
		<sex>Male</sex>
		<dmarks>Tight</dmarks>
		<sskills>Dancer</sskills>
		<bankno>101</bankno>
		<balance>0.00</balance>
	</spy>
	<spy>
		<firstName>Al</firstName>
		<surname>Capone</surname>
		<codeName>JB</codeName>
		<age>65</age>
		<sex>Male</sex>
		<dmarks>Fat</dmarks>
		<sskills>Dancer</sskills>
		<bankno>101</bankno>
		<balance>0.00</balance>
	</spy>
</spyList>
'************************************************

would be recieved as



rather than the current format of



Thanks a lot for any help that you can muster up.
 
Last edited by a moderator:
SelectNodes returns an XmlNodeList no matter what, and if you do For Each on a empty list nothing is done, so your "If ((Not xmlNlist Is Nothing) AndAlso (xmlNlist.Count > 0)) Then" is absolutely redundant.

So you do For Each Xspy In XList, if you want firstName node just get it, Xspy.SelectSingleNode("firstName")

You can also do some research about xml serialization, you can read/write all spy objects to/from an array basically with two lines of code. It is much easier handling the objects than fiddling with each property string. There's sample for example in this thread.
 

Similar threads

Back
Top