Mondeo Posted November 10, 2006 Posted November 10, 2006 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 Quote
Agent707 Posted November 10, 2006 Posted November 10, 2006 (edited) It is probably in this line NodeList = doc.GetElementsByTagName("GetVehicle") It's 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. <?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 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 <?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 '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. I'm outter here for the weekend. Good luck! Edited November 10, 2006 by Agent707 Quote
Mondeo Posted November 10, 2006 Author Posted November 10, 2006 I'd just like to say thanks. Your post is exactly what i've been looking for all night, I think I can get it working from here. Thanks again. Quote
Agent707 Posted November 11, 2006 Posted November 11, 2006 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 you're going on it now. Cheers :) Quote
Mondeo Posted November 11, 2006 Author Posted November 11, 2006 No problem, I can't 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 Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.