How to read XML

Mondeo

Well-known member
Joined
Nov 10, 2006
Messages
128
Location
Sunny Lancashire
I have the following simple XML File called getvehicles.xml

<?xml version="1.0" encoding="utf-8" ?>
<GetVehicles release="8.1" environment="Production" lang="en-US">
<ApplicationArea>
</ApplicationArea>
<DataArea>
<Error>
</Error>
<Vehicles>
<Vehicle>
<Combined_Make>VOLVO</Combined_Make>
<Combined_Model>70 SERIES C70 T5 GT</Combined_Model>
</Vehicle>
</Vehicles>
</DataArea>
</GetVehicles>

I need to read the combined _make and combined_model tags. I have this code

Dim

file As String = Server.MapPath("getvehicles.xml")
Dim doc As New XmlDocument
doc.Load(file)
Dim NodeList As XmlNodeList
Dim GetVehiclesNode As XmlNode
NodeList = doc.GetElementsByTagName("GetVehicle")
For Each GetVehiclesNode In NodeList
Response.Write(GetVehiclesNode.Attributes("Combined_Make").InnerText)
Response.Write(GetVehiclesNode.Attributes("Combined_Model").InnerText)
Next
But this returns an object not set to instance of an object error. What is the correct code.

Many thanks, this is my first try with XML
 
It is probably in this line
Code:
NodeList = doc.GetElementsByTagName("GetVehicle")
Its GetVehicles not GetVehicle

For starters, you need to understand the structure of an XML Document.
Nodes (Or tags as some prople call then) have a hierachy depending on where they are at.
Your document would look this if you indented it correctly.
Code:
<?xml version="1.0" encoding="utf-8" ?>
<GetVehicles release="8.1" environment="Production" lang="en-US">
	<ApplicationArea>
	</ApplicationArea>
	<DataArea>
		<Error></Error>
		<Vehicles>
			<Vehicle>
				<Combined_Make>VOLVO</Combined_Make>
				<Combined_Model>70 SERIES C70 T5 GT</Combined_Model>
			</Vehicle>
		</Vehicles>
	</DataArea>
</GetVehicles>
Another major thing. Combined_Model and Combined_Make are not attributes, they are Nodes.
Release, Environment and Lang are attributes (of GetVehicles)

You have to parse your nodes in the hierarchy they occurr in your document.

See if this makes sense
Code:
        Dim objNode As XmlNode
        Dim GetVehiclesNode As XmlNode
        Dim VehicleNode As XmlNode

        Get the root node of your document (where the release number matches if you want to).
        GetVehiclesNode = doc.SelectSingleNode("GetVehicles[@release=8.1]")

        Now get the Vehicle node that is nested down in your document like so.
        VehicleNode = GetVehiclesNode.SelectSingleNode("DataArea/Vehicles/Vehicle")

        Dim strNodeElement As String
        Dim strNodeText As String

        For Each objNode In VehicleNode
            strNodeElement = objNode.Name
            strNodeText = objNode.InnerText
        Next
Now, if you want your XML Document to have a more sensable structure, should probably do something more like this
Code:
<?xml version="1.0" encoding="utf-8" ?>
<GetVehicles release="8.1" environment="Production" lang="en-US">
	<ApplicationArea>
	</ApplicationArea>
	<DataArea>
		<Error></Error>
		<Vehicles>
			<Vehicle>
				<make>VOLVO</make>
				<model>70 SERIES C70 T5 GT</model>
			</Vehicle>
			<Vehicle>
				<make>BMW</make>
				<model>325i</model>
			</Vehicle>
			<Vehicle>
				<make>Volks Wagon</make>
				<model>Passat</model>			
			</Vehicle>
		</Vehicles>
	</DataArea>
</GetVehicles>
Then the code would be more like
Code:
        Now get the Vehicles node that is nested down in your document like so.
        VehiclesNode = GetVehiclesNode.SelectSingleNode("DataArea/Vehicles")

        Dim strMake As String
        Dim strModel As String

        For Each objNode In VehiclesNode.SelectNodes("Vehicle")
            strMake = objNode.SelectSingleNode("make").InnerText
            strModel = objNode.SelectSingleNode("model").InnerText
        Next
Hope that makes sense. Play with it and see how you do.

Im outter here for the weekend. Good luck!
 
Last edited by a moderator:
Id just like to say thanks.

Your post is exactly what ive been looking for all night, I think I can get it working from here.

Thanks again.
 
LOL! I just realized the structure of the 2nd document I posted is exactly the same as the one you had to begin with. Late in the day, what can I say but... Oh well.. Good that youre going on it now.

Cheers :)
 
No problem,

I cant change the structure of the XML file anyway becuase it comes from a 3rd party web service. And each XML stream only includes one <vehicle>, but with hundreds of fields.

Thanks again, its working!

Ben
 
Back
Top