Vb. NET deserialization of XML

Source: Internet
Author: User
Tags xpath xsl xslt

Serialization is the process of converting an object's state into a format that can be persisted or transmitted.

The inverse of serialization is deserialization. It converts the stream to an object. Together, the two processes are able to store and transmit data. This is where serialization is meant to be.


When you convert, process, and generate XML documents in vb.net. You need to use some classes in the XML private namespace that contain:

System.Xml This namespace provides a variety of XML standards, including the DTD, namespace, DOM, XDR (old version number of XML Data reduced,xml schema Standard), XPath, XSLT, and soap (which once represented simple Object The Access protocol standard, which today does not represent), is the core support.

System.Xml.Serialization objects provided by this namespace use serialization techniques to convert objects to and from Xml documents or streams.

System.Xml.Schema This namespace provides a set of objects for loading, creating, and outputting schemas that manipulate the various entities that make up the XML schema in memory

System.Xml.Xpath This namespace provides a parser and an estimation engine for Xpath (Xml Path Language).

System.Xml.Xsl This namespace provides an empty object that must be used when using XSL (extensible Stylesheet Language) and XSLT (XSL transformation).

System.Xml.Linq This namespace provides support for querying XML using LINQ.

What I want to talk about today is the vb.net deserialization of XML.


Here is the structure of the XML I want to deserialize:

<card><scodeborder y1= ' y2= ' x2= ' 568 ' x1= ' 0 '/><border ' y1= y2= ' 1008 ' x2= ' 1650 ' 0 ' x1=  Lt;area><question id= ' 1 ' x1= ' y1= ' 274 ' x2= ' "' y2= ' 288 ' ><option id= ' A ' x1= ' all ' y1= ' 274 ', ' x2= ' ' All ' y2= ' 288 ' /></question><question id= ' 2 ' x1= ' y1= ' 298 ' x2= ' "" y2= ' 312 ' ><option id= ' A ' x1= ' "y1= ' 298 ' x2= ' "Y2= ' 312 '/><option id= ' B ' x1= ' y1= ' 298 ' x2= ', ' '" ' y2= ', ' ' 312 ', '/></question><question id= ' 3 ' x1= ' 70 ' y1= ' 322 ' x2= ' y2= ' 336 ' ><option id= ' A ' x1= ' the ' y1= ', ' 322 ' x2= ' all ' y2= ' ' 336 '/><option id= ' B ' x1= ' ' y1= ' 3 * ' x2= ' y2= ' 336 '/><option id= ' C ' x1= ' "y1= ' 322 '" x2= ' "y2= </card>

The first thing we need to do is create the object corresponding to the XML, and then convert the XML into the object we want.

The System.Xml.Serialization namespace is required to be referenced in the created class.

This namespace contains classes for serializing objects into XML-formatted documents or streams.

Public Class card    <xmlelementattribute ("Scodeborder") > Public scodeborder as Scodeborder    < XmlElementAttribute ("border") > public border as Border    <xmlelementattribute (' area ') > Public area as area< C3/>public Sub New ()    End Sub Public    Sub new (ByVal Scodeborder as Scodeborder, ByVal border as border, ByVal area A s area)        Me.scodeborder = scodeborder        me.border = Border        Me.area = Area    End subend Class

Public Class Scodeborder    <xmlattributeattribute ("x1") > Public x1 as Integer    <xmlattributeattribute ( "X2") > Public x2 As Integer    <xmlattributeattribute ("y1") > Public y1 As Integer    < XmlAttributeAttribute ("y2") > Public y2 as Integer public    Sub New ()    End Sub Public    Sub New (ByVal X1 as Integ  Er, ByVal x2 As Integer, ByVal y1 As Integer, ByVal y2 as Integer)        me.x1 = x1        me.x2 = x2        me.y1 = y1        me.y2 = y2    End subend Class

Public Class Border    <xmlattributeattribute ("x1") > Public x1 as Integer    <xmlattributeattribute ("X2") > Public x2 As Integer    <xmlattributeattribute ("y1") > Public y1 As Integer    <xmlattributeattribute (" Y2 ") > Public y2 As Integer public    Sub New ()    End Sub Public    Sub New (ByVal x1 As Integer, ByVal x2 As Integer , ByVal y1 As Integer, ByVal y2 as Integer)        me.x1 = x1        me.x2 = x2        me.y1 = y1        me.y2 = y2    End subend C Lass

Public Class Area    <xmlelementattribute ("question") > Public ques_list () as question public    Sub New ()    End Sub    Public Sub New (ByVal Multiq () as question)        me.ques_list = Multiq    End subend Class


The object collection of the option class is maintained in the question class.


Public Class question    <xmlattributeattribute ("x1") > Public x1 as Integer    <xmlattributeattribute ("x2 ") > Public x2 As Integer    <xmlattributeattribute (" y1 ") > Public y1 As Integer    <xmlattributeattribute ("y2") > Public y2 as Integer    <xmlattributeattribute ("id") > Public id as String    < XmlElementAttribute ("option") > Public multioptions_list () as Option_ public    Sub New ()    End Sub    public Sub New (ByVal x1 As Integer, ByVal x2 As Integer, ByVal y2 as Integer, ByVal y1 As Integer, ByVal ID as String, ByVal mult IO () as Option_)        me.x1 = x1        me.x2 = x2        me.y2 = y2        me.y1 = y1        me.id = ID        me.multioptions_list = Multio    End subend Class

Public Class option_    <xmlattributeattribute ("x1") > Public x1 as Integer    <xmlattributeattribute ("X2" ) > Public x2 As Integer    <xmlattributeattribute ("y1") > Public y1 As Integer    <xmlattributeattribute ( "Y2") > Public y2 as Integer    <xmlattributeattribute ("id") > Public ID as String public    Sub New ()    End S UB Public    Sub New (ByVal x1 As Integer, ByVal x2 As Integer, ByVal y2 as Integer, ByVal y1 As Integer, ByVal ID as Stri NG)        me.x1 = x1        me.x2 = x2        me.y2 = y2        me.y1 = y1        me.id = ID    End subend Class

So the class for the XML to deserialize is set up. It's a little complicated, though. But with these classes, we don't have to deal with the nodes of the XML one by one ~

Verbose to the deserialized code. It's very easy. The code is as follows:

Dim cardxml As FileStream = new FileStream (Xmlpath, FileMode.Open) ' card is the class name, also the root node Dim serialize As XmlSerializer = new Xmlse Rializer (GetType (card)) Dim Wholecard as Card = serialize. Deserialize (Cardxml) Cardxml.close ()

The XML source for the above code is the file on disk.

But sometimes, XML is given in the form of a string.

How do we deal with this?

From the definition of deserialization. Deserialization is the transfer of a stream to an object.

The above code is deserialized in the form of a file stream. Assuming it is a string, we need to put the string into the memory stream and then deserialize the memory stream.

Dim descbytes () as Byte = System.Text.Encoding.Unicode.GetBytes (xmlstring) Dim serialize As XmlSerializer = New Xmlseriali Zer (GetType (card)) Dim Wholecard as Card = serialize. Deserialize (New MemoryStream (descbytes))

The above is the process of XML deserialization.


XML Serialization:

' Remove the XML declaration Dim settings As XmlWriterSettings = New xmlwritersettings () settings. Omitxmldeclaration = Truesettings.encoding = Encoding.defaultdim Mem as System.IO.MemoryStream = New MemoryStream () Using Writer as XmlWriter = XmlWriter.Create (mem, settings)' Remove the default namespace xmlns:xsd and Xmlns:xsiDim NSS as XmlSerializerNamespaces = New xmlserializernamespaces ()NSS. Add ("", "")Dim Formatter as XmlSerializer = New XmlSerializer (Wholecarda.gettype ())formatter. Serialize (writer, Wholecarda, NSS) End Usingdim resultstr As String = Encoding.Default.GetString (mem. ToArray ())

This method implements serialization of XML objects and the removal of XML declarations and default namespaces.



References: visual.basic.2010 & NET 4 Advanced Programming

Vb. NET deserialization of XML

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.