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 17

Written by: Steve Gray
3/17/2011 3:07 PM  RssIcon

I get to see quite a bit of XML, and my path in the past has always been to use LINQ to iterate through the document and read it into a class. Then I’d use the class to do whatever work I need to do.

This code takes advantage of the XmlSerializer.Deserialize method.

To run this code, create a Windows Forms application and drop this code into the form code behind. It is complete and should run unmodified.

Note that the OrderedItem class and the getXMLDoc function both make use of namespaces for two of the elements, so we show how to code this both with and without namespace references.

Code Snippet
  1. Imports System.Xml.Serialization
  2. Imports Microsoft.VisualBasic
  3.  
  4. Public Class Form1
  5.  
  6.     Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  7.         Try
  8.             ' Declare an object variable of the type to be deserialized.
  9.             Dim i As OrderedItem
  10.  
  11.             'Create an instance of the XmlSerializer specifying type and namespace.
  12.             'the 'OrderedItem' class is below
  13.             Dim serializer As New XmlSerializer(GetType(OrderedItem))
  14.  
  15.             'open an XmlReader from a string
  16.             'the getXMLDoc function returns an XML document in string format
  17.             Dim reader As System.Xml.XmlReader = System.Xml.XmlReader.Create(New System.IO.StringReader(getXMLDoc))
  18.  
  19.             ' Use the Deserialize method to restore the object's state.
  20.             i = CType(serializer.Deserialize(reader), OrderedItem)
  21.  
  22.             ' Write out the properties of the object.
  23.             Console.WriteLine("Item Name: " & i.ItemName)
  24.             Console.WriteLine("Item Desc: " & i.Description)
  25.  
  26.         Catch ex As Exception
  27.             MsgBox(ex.Message)
  28.         End Try
  29.  
  30.  
  31.     End Sub
  32.  
  33.     Function getXMLDoc() As String
  34.         Dim xElement As XElement
  35.         xElement = <OrderedItem xmlns:money="http://www.devshed.us">
  36.                        <ItemName>Widget</ItemName>
  37.                        <Description>Regular Widget</Description>
  38.                        <Quantity>10</Quantity>
  39.                        <money:UnitPrice>2.3</money:UnitPrice>
  40.                        <money:LineTotal>23</money:LineTotal>
  41.                    </OrderedItem>
  42.  
  43.         Return xElement.ToString
  44.  
  45.     End Function
  46. End Class
  47.  
  48. ' This is the class that will be deserialized.
  49. Public Class OrderedItem
  50.     <XmlElement()> _
  51.     Public ItemName As String
  52.  
  53.     <XmlElement()> _
  54.     Public Description As String
  55.  
  56.     <XmlElement()> _
  57.     Public Quantity As Integer
  58.  
  59.     <XmlElement(Namespace:="http://www.devshed.us")> _
  60.     Public UnitPrice As Decimal
  61.  
  62.     <XmlElement(Namespace:="http://www.devshed.us")> _
  63.     Public LineTotal As Decimal
  64.     ' A custom method used to calculate price per item.
  65.     Public Sub Calculate()
  66.         LineTotal = UnitPrice * Quantity
  67.     End Sub
  68. 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