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 15

Written by: Steve Gray
3/15/2011 3:27 PM  RssIcon

This code sample will show you how to take an XML document and parse it into VB elements (in this example I use a class)

First create the class:

Code Snippet
  1. Public Class Item
  2.     Public Property itemnmbr As String
  3.     Public Property itemguid As Guid
  4.     Public Property active As String
  5.     Public Property description As String
  6.     Public Property category As String
  7.     Public Property commissionamt As Double
  8.     Public Property listprice As Double
  9.     Public Property reorderpt As Int32
  10.  
  11. End Class

 

This code will take the XML document shown are read it into the class. You’d want a Generic Collection of this class to get all the items, I’m going for simplicity here as much as possible

Code Snippet
  1. Dim xDoc As XDocument
  2. Dim xElem As XElement
  3.  
  4. xElem = <items>
  5.             <item guid="6A639774-5EA9-440C-978B-FDC7DBF71949">
  6.                 <SKUid>AAA001</SKUid>
  7.                 <active>Y</active>
  8.                 <description>SD128 RAM</description>
  9.                 <category>Electronics</category>
  10.                 <commissionamt>0.00</commissionamt>
  11.                 <listprice>9.99</listprice>
  12.                 <reorderpt>1</reorderpt>
  13.             </item>
  14.             <item guid="D8FD406F-8CF2-47E6-B09D-E44106153C11">
  15.                 <SKUid>AAA02</SKUid>
  16.                 <active>Y</active>
  17.                 <description>SD256 RAM</description>
  18.                 <category>Electronics</category>
  19.                 <commissionamt>0.00</commissionamt>
  20.                 <listprice>19.99</listprice>
  21.                 <reorderpt>5</reorderpt>
  22.             </item>
  23.         </items>
  24.  
  25. xDoc = XDocument.Parse(xElem.ToString)
  26.  
  27. Dim childElements As IEnumerable(Of XElement)
  28.  
  29. childElements = _
  30.     From el In xDoc.<items>.Elements() _
  31.     Select el
  32.  
  33. For Each el As XElement In childElements
  34.     oItem = New Item
  35.     oItem.itemguid = New Guid(el.@guid)
  36.     oItem.itemnmbr = el.<SKUid>.Value
  37.     oItem.active = el.<active>.Value
  38.     oItem.description = el.<description>.Value
  39.     oItem.category = el.<category>.Value
  40.     oItem.commissionamt = el.<commissionamt>.Value
  41.     oItem.listprice = el.<listprice>.Value
  42.     oItem.reorderpt = el.<reorderpt>.Value
  43. Next

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