XmlSerializer in VB.NET

XmlSerializer enables you to control how objects are encoded into XML, it has a number of constructors. If you use any of the constructors.
  • 10350

The XmlSerializer enables you to control how objects are encoded into XML, it has a number of constructors. If you use any of the constructors other than the one that takes a type then a new temporary assembly is created EVERY TIME you create a serializer, rather than only once. It allows you to serializeand deserialize objects into and from XML documents. It enables youto control how objects are encoded into XML. The class constructoraccepts the object type to serialize.

The constructor which is used is below in action:

Dim serialization As XmlSerializer = New XmlSerializer(GetType(Product))

The list of XmlSerializer members are given below:

  • XmlSerializer
  • XmlSerializer(Type)
  • XmlSerializer(XmlTypeMapping)
  • XmlSerializer(Type, String)
  • XmlSerializer(Type, Type())
  • XmlSerializer(Type, XmlAttributeOverrides)
  • XmlSerializer(Type, XmlRootAttribute)
  • XmlSerializer(Type, XmlAttributeOverrides, Type(), XmlRootAttribute, String)
  • XmlSerializer(Type, XmlAttributeOverrides, Type(), XmlRootAttribute, String, String)
  • XmlSerializer(Type, XmlAttributeOverrides, Type(), XmlRootAttribute, String, String, Evidence)

The role of XmlSerializer in a standard ASP.NET Web method invocation

  xml1.gif

The central class of the System.Xml.Serialization namespace is XmlSerializer. It contains classes for working with the various aspects of XML, below we listed all methods which used with XmlSerializer in various XML aspects.

  • CanDeserialize
  • CreateReader
  • CreateWriter
  • Deserialize(Stream)
  • Deserialize(TextReader)
  • Deserialize(XmlReader)
  • Deserialize(XmlSerializationReader)
  • Deserialize(XmlReader, String)
  • Deserialize(XmlReader, XmlDeserializationEvents)
  • Deserialize(XmlReader, String, XmlDeserializationEvents)
  • Equals(Object)
  • Finalize
  • FromMappings(XmlMapping())
  • FromMappings(XmlMapping(), Evidence)
  • FromMappings(XmlMapping(), Type) I
  • FromTypes
  • GenerateSerializer(Type(), XmlMapping())
  • GenerateSerializer(Type(), XmlMapping(), CompilerParameters)
  • GetHashCode
  • GetType
  • GetXmlSerializerAssemblyName(Type)
  • GetXmlSerializerAssemblyName(Type, String)
  • MemberwiseClone
  • Serialize(Object, XmlSerializationWriter)
  • Serialize(Stream, Object)
  • Serialize(TextWriter, Object)
  • Serialize(XmlWriter, Object)
  • Serialize(Stream, Object, XmlSerializerNamespaces)
  • Serialize(TextWriter, Object, XmlSerializerNamespaces)
  • Serialize(XmlWriter, Object, XmlSerializerNamespaces)
  • Serialize(XmlWriter, Object, XmlSerializerNamespaces, String)
  • Serialize(XmlWriter, Object, XmlSerializerNamespaces, String, String)
  • ToString

Lets look an example to see the functionality of XmlSerializer using in VB.NET

Example

Imports
System.Xml
Imports System.Xml.Serialization
Imports System.IO 
 
Public Class Main
 
    Public Shared Sub Main()
        Dim serialization As XmlSerializer = New XmlSerializer(GetType(Product))
        Dim car As Product = New Product("BMW", 1, 10000000)
 
        serialization.Serialize(Console.Out, car)
        Console.ReadLine()
 
    End Sub
End Class 
 
Public Class Product
 
    <XmlElementAttribute("Name")> Public name As String
    <XmlAttributeAttribute("Quantity")> Public quantity As Integer
    <XmlAttributeAttribute("Price")> Public price As Integer
 
    Public Sub New()
    End Sub
 
    Public Sub New(ByVal name As String, _
                   ByVal quantity As Integer, _
                   ByVal price As Integer)
        Me.name = name
        Me.quantity = quantity
        Me.price = price
    End Sub
End Class

Output

xml2.gif

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.