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:53 PM  RssIcon

In the last example we took a simple XML document and deserialized it into a class. This example takes that idea one step further. Our source XML document is a standard ‘order’ with an Order Header and Order Detail sections. The class that we’re going to send it into needs to have a Generic List (of Item) to hold the order details.

Also, in the last example each item in the class was decorated with an <XElement> directive. In this example we’ve remove those and placed a <Serializable> directive at the top of the class.

Also note that the <XmlAttribute> was applied to the OrderNumber property, and it got correctly picked up as an attribute in the XML document

 

Code Snippet
  1. Imports System.Xml.Serialization
  2. Imports Microsoft.VisualBasic
  3.  
  4. Public Class Form2
  5.  
  6.     Private Sub Form2_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 Order
  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(Order))
  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), Order)
  21.  
  22.             ' Write out the properties of the object.
  23.             Console.WriteLine("Item Name: " & i.OrderNumber)
  24.             Console.WriteLine("Item Desc: " & i.CustomerNumber)
  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 = <Order OrderNumber="ORD002">
  36.                        <CustomerNumber>CUST001</CustomerNumber>
  37.                        <Items>
  38.                            <Item>
  39.                                <ItemName>Widget</ItemName>
  40.                                <Quantity>1</Quantity>
  41.                                <UnitPrice>9</UnitPrice>
  42.                            </Item>
  43.                            <Item>
  44.                                <ItemName>Widget</ItemName>
  45.                                <Quantity>10</Quantity>
  46.                                <UnitPrice>2.3</UnitPrice>
  47.                            </Item>
  48.                        </Items>
  49.                    </Order>
  50.  
  51.         Return xElement.ToString
  52.  
  53.     End Function
  54. End Class
  55.  
  56. 'This is the class that will be deserialized.
  57. <Serializable()> _
  58. Public Class Order
  59.     <XmlAttribute()> _
  60.     Public OrderNumber As String
  61.  
  62.     Public CustomerNumber As String
  63.     Public Items As List(Of Item)
  64.  
  65. End Class
  66.  
  67. <Serializable()> _
  68. Public Class Item
  69.     Public ItemName As String
  70.     Public Quantity As Integer
  71.     Public UnitPrice As Decimal
  72. 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