Mar
16
Written by:
Steve Gray
3/16/2011 3:47 PM
The task here is to take an XML document in this form:
<transfers>
<transfer transferNumber="TSFR001">
<timestamp>2011-03-14 11:24:47.077</timestamp>
<items>
<item>
<itemguid>6A639774-5EA9-440C-978B-FDC7DBF71949</itemguid>
<quantity>1</quantity>
<fromsite>001</fromsite>
<tosite>002</tosite>
</item>
<item>
<itemguid>D8FD406F-8CF2-47E6-B09D-E44106153C11</itemguid>
<quantity>1</quantity>
<fromsite>001</fromsite>
<tosite>002</tosite>
</item>
</items>
</transfer>
<transfer transferNumber="TSFR002">
<timestamp>2011-03-14 11:24:47.077</timestamp>
<items>
<item>
<itemguid>6A639774-5EA9-440C-978B-FDC7DBF71949</itemguid>
<quantity>2</quantity>
<fromsite>001</fromsite>
<tosite>002</tosite>
</item>
<item>
<itemguid>D8FD406F-8CF2-47E6-B09D-E44106153C11</itemguid>
<quantity>2</quantity>
<fromsite>001</fromsite>
<tosite>002</tosite>
</item>
</items>
</transfer>
</transfers>
And parse it and move it into a class that we can manipulate in VB.
The class structure mirrors the XML document:
Code Snippet
- Public Class Transfer
- Public transferNumber
- Public timestamp As DateTime
- Public transferDetail As List(Of TransferDetail)
- Public test As List(Of String)
-
- End Class
-
- Public Class TransferDetail
- Public itemguid As Guid
- Public quantity As Double
- Public fromsite As String
- Public tosite As String
- End Class
Our code loads the XML into an XDocument, and then uses LINQ queries to read it into an instance of the class.
Code Snippet
- Sub example()
- Dim xDoc As XDocument
- Dim eDoc As XElement
- Dim oTransfer As Transfer
- Dim oTransferDetail As TransferDetail
-
- 'load an XML document into an xElement type
- eDoc = <transfers>
- <transfer transferNumber="TSFR001">
- <timestamp>2011-03-14 11:24:47.077</timestamp>
- <items>
- <item>
- <itemguid>6A639774-5EA9-440C-978B-FDC7DBF71949</itemguid>
- <quantity>1</quantity>
- <fromsite>001</fromsite>
- <tosite>002</tosite>
- </item>
- <item>
- <itemguid>D8FD406F-8CF2-47E6-B09D-E44106153C11</itemguid>
- <quantity>1</quantity>
- <fromsite>001</fromsite>
- <tosite>002</tosite>
- </item>
- </items>
- </transfer>
- <transfer transferNumber="TSFR002">
- <timestamp>2011-03-14 11:24:47.077</timestamp>
- <items>
- <item>
- <itemguid>6A639774-5EA9-440C-978B-FDC7DBF71949</itemguid>
- <quantity>2</quantity>
- <fromsite>001</fromsite>
- <tosite>002</tosite>
- </item>
- <item>
- <itemguid>D8FD406F-8CF2-47E6-B09D-E44106153C11</itemguid>
- <quantity>2</quantity>
- <fromsite>001</fromsite>
- <tosite>002</tosite>
- </item>
- </items>
- </transfer>
- </transfers>
-
- 'read the xElement into an xDocument. This is the easiest and clearest way to code the example
- xDoc = XDocument.Parse(eDoc.ToString)
-
- 'create a collection of xElements
- Dim transferNodes As IEnumerable(Of XElement)
-
- 'LINQ query to read the TRANSFERS into our collection
- transferNodes = _
- From el In xDoc.<transfers>.Elements() _
- Select el
-
- 'loop through the collection of TRANSFERS
- For Each el As XElement In transferNodes
- 'delare a new transfer class
- oTransfer = New Transfer
-
- 'initialize the transferDetail Generic.List
- oTransfer.transferDetail = New List(Of TransferDetail)
-
- 'assign some of the transfer class properties. Note how we get the transferNumber attribute here
- oTransfer.transferNumber = el.@transferNumber
- oTransfer.timestamp = el.<timestamp>.Value
-
- 'the transfer document has childern - items
- 'declare a collection of items
- Dim itemNodes As IEnumerable(Of XElement)
-
- 'LINQ query to loop through the collection of item elements
- itemNodes = From ie In el.<items>.Elements _
- Select ie
-
- 'loop through the colleciton of ITEMS
- For Each ie As XElement In itemNodes
- 'delcare a new transfer detail class
- oTransferDetail = New TransferDetail
-
- 'assign some of the class properties
- oTransferDetail.itemguid = New Guid(ie.<itemguid>.Value)
- oTransferDetail.quantity = ie.<quantity>.Value
- oTransferDetail.tosite = ie.<tosite>.Value
- oTransferDetail.fromsite = ie.<fromsite>.Value
-
- 'add the new transferdetail class to the parent transfer class
- oTransfer.transferDetail.Add(oTransferDetail)
- Next
- Next
-
-
-
-
- End Sub
- End Class
As always, I welcome your comments!