I need some help figuring out how to parse an XML file in VB.net so I can find a particular node and write the contents into a database. Basically all the data in the XML node below needs to be read into an object. The file contains hundreds of these nodes

<Product ID="523233" UserTypeID="Property" ParentID="523232">
	<Name>My Property Name</Name>                     
	<AssetCrossReference AssetID="173501" Type=" Non Print old">
	  </AssetCrossReference>
	  <AssetCrossReference AssetID="554740" Type=" Non Print old">
	  </AssetCrossReference>
	  <AssetCrossReference AssetID="566495" Type=" Non Print old">
	  </AssetCrossReference>
	  <AssetCrossReference AssetID="553014" Type="Non Print">
	  </AssetCrossReference>
	  <AssetCrossReference AssetID="553015" Type="Non Print">
	  </AssetCrossReference>
	  <AssetCrossReference AssetID="553016" Type="Non Print">
	  </AssetCrossReference>
	  <AssetCrossReference AssetID="553017" Type="Non Print">
	  </AssetCrossReference>
	  <AssetCrossReference AssetID="553018" Type="Non Print">
	  </AssetCrossReference>

	<Values>
	  <Value AttributeID="5115">Section of main pool</Value>
	  <Value AttributeID="5137">114 apartments, four floors, no lifts</Value>
	  <Value AttributeID="5170">Property location</Value>
	  <Value AttributeID="5164">2 key</Value>
	  <Value AttributeID="5134">A comfortable property, the apartment is set on a pine-covered hillside - a scenic and peaceful location.</Value>
	  <Value AttributeID="5200">PROPERTY_ID</Value>
	  <Value AttributeID="5148">facilities include X,Y,Z</Value>
	  <Value AttributeID="5067">Self Catering. </Value>
	  <Value AttributeID="5221">Frequent organised daytime activities</Value>
	</Values>
  </Product>
</Product>

I've had some success playing around with some Linq code, but I'm not able to figure out a way to traverse the sub nodes, so I can read them into an object.

Dim productsXML As XElement = XElement.Load("C:\properties.XML)
Dim ParentNode As XElement

Dim Query = From p In productsXML...<Value> _
	Where p.Value = "PROPERTY_ID"
ParentNode = Query.FirstOrDefault()

Why not load the XML file into an XmlDocument?
From there you can create an XmlNodelist and iterate through it until you find the node you're looking for.
When you find it you can extract that XmlElement from the XmlNodelist and add some logic to extract the values.
This is a quick and dirty example.

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim doc As XmlDocument = New XmlDocument
        doc.Load("xmlFile.xml")
        Dim nodeList As XmlNodeList = doc.GetElementsByTagName("Product")
        LoopThroughXmlDoc(nodeList)
    End Sub

    Private Sub LoopThroughXmlDoc(ByVal nodeList As XmlNodeList)
        For Each elem As XmlElement In nodeList
            If elem.HasChildNodes Then
                LoopThroughXmlDoc(elem.ChildNodes)
            Else
                '' Extract the information
                If elem.HasAttribute("AssetID") Then
                    'elem.Attributes("AssetID").Value.ToString()
                ElseIf elem.HasAttribute("AttributeID") Then
                    'elem.Attributes("AttributeID").Value.ToString()
                End If
            End If
        Next
    End Sub

I know this is old, but this has helped me a lot!, thank you very much.

you are a code saver indeed.

This article has been dead for over six months. Start a new discussion instead.