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!
Apr 4

Written by: Steve Gray
4/4/2011 5:45 PM  RssIcon

Here is a simple code example showing how to take a VB Class object and us Xml.Serialization to turn it into an XML string. I had a little trouble finding this code because all the examples wanted to write the XML to disk using a StreamWriter… I needed it in memory as a string

The resulting string looks like  this:

SNAGHTML1b80ab8a

 

Code Snippet
  1. Public Module Module1
  2.  
  3.     Public Class Product
  4.         'this is our simple class of 'product' that we'll create
  5.         Public Property ProductID As String
  6.         Public Property ProductName As String
  7.         Public Property cost As Double
  8.     End Class
  9.     Public Sub Main()
  10.  
  11.         'to make it a little more interesting, we'll serialize an Array of Product
  12.         'declare the array
  13.         Dim oProducts(1) As Product
  14.         'declare the product class
  15.         Dim oProduct As Product
  16.  
  17.         'create the first product
  18.         oProduct = New Product
  19.         oProduct.ProductID = "1"
  20.         oProduct.ProductName = "Hammer"
  21.         oProduct.cost = 5.12
  22.         oProducts(0) = oProduct
  23.  
  24.         'create the second product
  25.         oProduct = New Product
  26.         oProduct.ProductID = "2"
  27.         oProduct.ProductName = "Chisel"
  28.         oProduct.cost = 9.99
  29.         oProducts(1) = oProduct
  30.  
  31.         'Serialize the array to a string
  32.         Dim sw As New IO.StringWriter
  33.         Dim oSerializer As New Xml.Serialization.XmlSerializer(oProducts.GetType)
  34.         oSerializer.Serialize(sw, (oProducts))
  35.         MsgBox(sw.ToString)
  36.  
  37.     End Sub
  38. End Module

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