Jun
17
Written by:
Steve Gray
6/17/2010 12:02 PM
I frequently have to read from XML files, the general format of them is something like this:
<eConnect xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<POPReceivingsType>
<taUpdateCreateVendorRcd>
</taUpdateCreateVendorRcd>
<taCreateVendorAddress>
</taCreateVendorAddress>
<taPopRcptLineInsert_Items>
<taPopRcptLineInsert>
<PONUMBER>PO08/00012</PONUMBER>
<LOCNCODE>VA-PIEDMONTB</LOCNCODE>
</taPopRcptLineInsert>
<taPopRcptLineInsert>
<PONUMBER>PO08/00012</PONUMBER>
<LOCNCODE>VA-PIEDMONTA</LOCNCODE>
</taPopRcptLineInsert>
</taPopRcptLineInsert_Items>
<taPopRcptHdrInsert>
<POPRCTNM>RC08/00028</POPRCTNM>
<POPTYPE>1</POPTYPE>
</taPopRcptHdrInsert>
</POPReceivingsType>
</eConnect>
Here’s a simple piece of code that will read the first <taPopRcptLineInsert> node and
give me the element contained in it.
In earlier .NET Framework versions I had to set references and include ‘IMPORTS’
directives, 4.0 is not requiring that.
Public Class Form2
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles MyBase.Load
test()
End Sub
Private Sub test()
Dim strPOnumber As String
Dim strLOCNCODE As String
Dim doc As System.Xml.Linq.XDocument = _
XDocument.Load(Application.StartupPath & "\xmlfile1.xml")
Dim query = _
From c In doc.<eConnect>.<POPReceivingsType>.<taPopRcptLineInsert_Items> _
Select c.Nodes.First
For Each g As XElement In query
strPOnumber = g.<PONUMBER>(0).Value
strLOCNCODE = g.<LOCNCODE>(0).Value
Next
End Sub
End Class
As always, I welcome your comments!