Vishwamohan

Welcome to Vishwa's blog - Technology, Spirituality and More...

Using XMLWriter and XMLReader

This is a simple example for demonstrating how to use XMLReader or XMLWriter for reading or writing a XML file or XML String. I am using a Customer Entity which was created in previous examples.

XML File/String:

      <?xml version="1.0" encoding="utf-8" standalone="yes" ?>
          <XML_RESPONSE STATUS="1" DESCRIPTION="Success">
                   <CUSTOMERS>
                              <CUSTOMER ID="1" NAME="Joe Smith" DOB="1/1/1980" />
                              <CUSTOMER ID="2" NAME="Anita Wang" DOB="1/1/1982" />
                    </CUSTOMERS>
       </XML_RESPONSE>
         
      Required Imports Statements
      Imports System.Xml 
      Imports System.IO

      XML Writer:

           Private Function CreateCustomerXML(ByVal statusID As Integer, _
                                    ByVal statusDescription As String, _ 

                                    ByVal customers As List(Of BECustomer)) As String

        Dim xmlDocString As New System.Text.StringBuilder

        Dim xmlWriterSettings As New XmlWriterSettings()

        xmlWriterSettings.Indent = True

        xmlWriterSettings.IndentChars = vbTab

 

        Using xmlDocWriter As XmlWriter = XmlWriter.Create(xmlDocString, xmlWriterSettings)

            'Using xmlDocWriter As XmlWriter = XmlWriter.Create("C:\Temp\CustXml.xml")

            xmlDocWriter.WriteStartDocument(True)

            xmlDocWriter.WriteStartElement("XML_RESPONSE")

            xmlDocWriter.WriteAttributeString("STATUS", statusID.ToString)

            xmlDocWriter.WriteAttributeString("DESCRIPTION", statusDescription)

 

            xmlDocWriter.WriteStartElement("CUSTOMERS")

            For i As Integer = 0 To customers.Count - 1

                xmlDocWriter.WriteStartElement("CUSTOMER")

                xmlDocWriter.WriteAttributeString("ID", customers(i).CustID.ToString)

                xmlDocWriter.WriteAttributeString("NAME", customers(i).CustName.ToString)

                xmlDocWriter.WriteAttributeString("DOB", customers(i).CustDOB.ToShortDateString)

                xmlDocWriter.WriteEndElement()

            Next

 

            xmlDocWriter.WriteEndElement()

            xmlDocWriter.WriteEndDocument()

            xmlDocWriter.Close()

        End Using

 

        Return xmlDocString.ToString()

    End Function

     XML Reader:

 

    Private Function ReadCustomerXML(ByVal xmlDocString As String) As List(Of BECustomer)

        Dim customers As New List(Of BECustomer)

        Dim status As Integer = 0

        Dim statusDescription As String = String.Empty

        Dim uniEncoding As New System.Text.UnicodeEncoding()

        Dim stringBytes As Byte() = uniEncoding.GetBytes(xmlDocString)

        Dim xmlDocSteam As New MemoryStream(stringBytes, 0, stringBytes.Length)

 

        Using xmlDocReader As XmlReader = XmlReader.Create(xmlDocSteam)

            'Using xmlDocReader As XmlReader = XmlReader.Create("C:\Temp\Custxml.xml")

            xmlDocReader.Read()

            xmlDocReader.MoveToContent()

            If xmlDocReader.HasAttributes Then

                xmlDocReader.MoveToAttribute("STATUS")

                status = CInt(xmlDocReader.Value)

                xmlDocReader.MoveToAttribute("DESCRIPTION")

                statusDescription = xmlDocReader.Value

            End If

 

            While xmlDocReader.Read()

                If xmlDocReader.NodeType = XmlNodeType.Element _

                      AndAlso xmlDocReader.Name = "CUSTOMER" Then

                    Dim custRec As New BECustomer

                    If xmlDocReader.HasAttributes Then

                        xmlDocReader.MoveToAttribute("ID")

                        custRec.CustID = CInt(xmlDocReader.Value)

                        xmlDocReader.MoveToAttribute("NAME")

                        custRec.CustName = xmlDocReader.Value

                        xmlDocReader.MoveToAttribute("DOB")

                        custRec.CustDOB = CDate(xmlDocReader.Value)

                        customers.Add(custRec)

                    End If

                End If

            End While

        End Using

        xmlDocSteam.Close()

 

        Return customers

 

      End Function

 

 

     Test :

 

       Private Sub TestReadWriteXML()

 

        Dim xmlDocString As String = String.Empty

        Dim custs As New List(Of BECustomer)

 

        Dim cust1 As New BECustomer(1, "Joe Smith", #1/1/1980#)

        custs.Add(cust1)

        Dim cust2 As New BECustomer(2, "Anita Wang", #1/1/1982#)

        custs.Add(cust2)

 

        'Create XML String

        xmlDocString = CreateCustomerXML(1, "Success", custs)

        MsgBox(xmlDocString)

 

        'Read XML String

        custs = ReadCustomerXML(xmlDocString)

        MsgBox(custs.Count.ToString)

       End Sub

 

Loading