Login    
 
 
 
 
Text/HTML
  
You are here :: Blogs Saturday, May 19, 2012

Search
Note: This uses the internal blog search engine. The Google search engine is also available at the top of the page.
  
Disclaimer

Please review the site disclaimer before downloading or using content found on this site

  
Categories
  
DEVSHED Blog
As always, I welcome your comments!
Mar 16

Written by: Steve Gray
3/16/2011 3:47 PM  RssIcon

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
  1. Public Class Transfer
  2.     Public transferNumber
  3.     Public timestamp As DateTime
  4.     Public transferDetail As List(Of TransferDetail)
  5.     Public test As List(Of String)
  6.  
  7. End Class
  8.  
  9. Public Class TransferDetail
  10.     Public itemguid As Guid
  11.     Public quantity As Double
  12.     Public fromsite As String
  13.     Public tosite As String
  14. 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
  1.     Sub example()
  2.         Dim xDoc As XDocument
  3.         Dim eDoc As XElement
  4.         Dim oTransfer As Transfer
  5.         Dim oTransferDetail As TransferDetail
  6.  
  7.         'load an XML document into an xElement type
  8.         eDoc = <transfers>
  9.                    <transfer transferNumber="TSFR001">
  10.                        <timestamp>2011-03-14 11:24:47.077</timestamp>
  11.                        <items>
  12.                            <item>
  13.                                <itemguid>6A639774-5EA9-440C-978B-FDC7DBF71949</itemguid>
  14.                                <quantity>1</quantity>
  15.                                <fromsite>001</fromsite>
  16.                                <tosite>002</tosite>
  17.                            </item>
  18.                            <item>
  19.                                <itemguid>D8FD406F-8CF2-47E6-B09D-E44106153C11</itemguid>
  20.                                <quantity>1</quantity>
  21.                                <fromsite>001</fromsite>
  22.                                <tosite>002</tosite>
  23.                            </item>
  24.                        </items>
  25.                    </transfer>
  26.                    <transfer transferNumber="TSFR002">
  27.                        <timestamp>2011-03-14 11:24:47.077</timestamp>
  28.                        <items>
  29.                            <item>
  30.                                <itemguid>6A639774-5EA9-440C-978B-FDC7DBF71949</itemguid>
  31.                                <quantity>2</quantity>
  32.                                <fromsite>001</fromsite>
  33.                                <tosite>002</tosite>
  34.                            </item>
  35.                            <item>
  36.                                <itemguid>D8FD406F-8CF2-47E6-B09D-E44106153C11</itemguid>
  37.                                <quantity>2</quantity>
  38.                                <fromsite>001</fromsite>
  39.                                <tosite>002</tosite>
  40.                            </item>
  41.                        </items>
  42.                    </transfer>
  43.                </transfers>
  44.  
  45.         'read the xElement into an xDocument. This is the easiest and clearest way to code the example
  46.         xDoc = XDocument.Parse(eDoc.ToString)
  47.  
  48.         'create a collection of xElements
  49.         Dim transferNodes As IEnumerable(Of XElement)
  50.  
  51.         'LINQ query to read the TRANSFERS into our collection
  52.         transferNodes = _
  53.             From el In xDoc.<transfers>.Elements() _
  54.             Select el
  55.  
  56.         'loop through the collection of TRANSFERS
  57.         For Each el As XElement In transferNodes
  58.             'delare a new transfer class
  59.             oTransfer = New Transfer
  60.  
  61.             'initialize the transferDetail Generic.List
  62.             oTransfer.transferDetail = New List(Of TransferDetail)
  63.  
  64.             'assign some of the transfer class properties. Note how we get the transferNumber attribute here
  65.             oTransfer.transferNumber = el.@transferNumber
  66.             oTransfer.timestamp = el.<timestamp>.Value
  67.  
  68.             'the transfer document has childern - items
  69.             'declare a collection of items
  70.             Dim itemNodes As IEnumerable(Of XElement)
  71.  
  72.             'LINQ query to loop through the collection of item elements
  73.             itemNodes = From ie In el.<items>.Elements _
  74.                            Select ie
  75.  
  76.             'loop through the colleciton of ITEMS
  77.             For Each ie As XElement In itemNodes
  78.                 'delcare a new transfer detail class
  79.                 oTransferDetail = New TransferDetail
  80.  
  81.                 'assign some of the class properties
  82.                 oTransferDetail.itemguid = New Guid(ie.<itemguid>.Value)
  83.                 oTransferDetail.quantity = ie.<quantity>.Value
  84.                 oTransferDetail.tosite = ie.<tosite>.Value
  85.                 oTransferDetail.fromsite = ie.<fromsite>.Value
  86.  
  87.                 'add the new transferdetail class to the parent transfer class
  88.                 oTransfer.transferDetail.Add(oTransferDetail)
  89.             Next
  90.         Next
  91.  
  92.  
  93.  
  94.  
  95.     End Sub
  96. End Class

Tags:
Categories:
As always, I welcome your comments!
  
 
 
Home | Products | Blogs | Contact Us | Links | God's Plan
Privacy Statement | Terms Of Use
 
Copyright 2011 by Devshed.us