You can choose to create a XML file in which detail records are represented as Elements or Attributes only. It is wiser to use the best use of both, more use of attribute reduces the size of xml file and increases the speed over wire. Representation of data in Elements only XML file becomes heavier in file size thus slower the transmission. The only benefit is when you open this file in browser it is relatively easily to read. Following are an example for creating a file with same information in two different ways.
 

XML Element Format

This format takes more file size, but increases the readability of the file in browser. This example can be used for gathering all the exceptions generated during application uses.

 Private Sub LogErrorAsXMLFileElement(ByVal excep As Exception, ByVal serverName As String, _

         Optional ByVal remoteHostAddress As String = "", Optional ByVal referer As String = "")

 

        Dim xmlDataDoc As New XmlDataDocument

        Dim xmlDeclare As XmlDeclaration = Nothing

        Dim rootNode As XmlNode = Nothing

        Dim childElement As XmlElement = Nothing

        Dim parentElement As XmlElement = Nothing

 

        Try

            Dim rootTag As String = "Errors." & serverName.ToLower

            Dim filePathName As String = WebConfigurationManager.AppSettings("ErrorLogFolder") & "\" & _

                         serverName & ".ErrorLog." & Format(Now(), "yyyyMM").ToString & "E.xml"

 

 

            If File.Exists(filePathName) Then

                xmlDataDoc.Load(filePathName)

                rootNode = xmlDataDoc.GetElementById(rootTag)

            Else

                xmlDeclare = xmlDataDoc.CreateXmlDeclaration("1.0", Nothing, Nothing)

                rootNode = xmlDataDoc.CreateElement(rootTag)

                xmlDataDoc.AppendChild(rootNode)

                xmlDataDoc.InsertBefore(xmlDeclare, rootNode)

            End If

 

            childElement = xmlDataDoc.CreateElement("Error", "")

            rootNode.AppendChild(childElement)

 

            parentElement = childElement

            childElement = xmlDataDoc.CreateElement("ID", "")

            childElement.InnerText = XmlConvert.ToString(Guid.NewGuid)

            parentElement.AppendChild(childElement)

 

            childElement = xmlDataDoc.CreateElement("DateCreated", "")

            childElement.InnerText = XmlConvert.ToString(Now, XmlDateTimeSerializationMode.Local)

            parentElement.AppendChild(childElement)

 

            childElement = xmlDataDoc.CreateElement("RemoteHostAddress", "")

            childElement.InnerText = remoteHostAddress

            parentElement.AppendChild(childElement)

 

            childElement = xmlDataDoc.CreateElement("Referer", "")

            childElement.InnerText = referer

            parentElement.AppendChild(childElement)

 

            childElement = xmlDataDoc.CreateElement("Message", "")

            childElement.InnerText = excep.Message

            parentElement.AppendChild(childElement)

 

            childElement = xmlDataDoc.CreateElement("Exception", "")

            childElement.InnerText = excep.ToString

            parentElement.AppendChild(childElement)

 

            xmlDataDoc.Save(filePathName)

 

        Catch exc As Exception

            Throw New Exception(exc.Message)

        Finally

            xmlDataDoc = Nothing

        End Try

 

    End Sub

 

XML Attribute Format

This format takes less file size, but decreases the readability of the file in browser. This example can be used for gathering all the exceptions generated during application uses.

Private Sub LogErrorAsXMLFileAttribute(ByVal excep As Exception, ByVal serverName As String, _

Optional ByVal remoteHostAddress As String = "", Optional ByVal referer As String = "")

 

        Dim xmlDataDoc As New XmlDataDocument

        Dim xmlDeclare As XmlDeclaration = Nothing

        Dim rootNode As XmlNode = Nothing

        Dim childElement As XmlElement = Nothing

 

 

        Try

            Dim rootTag As String = "Errors." & serverName.ToLower

            Dim filePathName As String = WebConfigurationManager.AppSettings("ErrorLogFolder") & "\" & _

                            serverName & ".ErrorLog." & Format(Now(), "yyyyMM").ToString & "A.xml"

 

            If File.Exists(filePathName) Then

                xmlDataDoc.Load(filePathName)

                rootNode = xmlDataDoc.SelectSingleNode(rootTag)

            Else

                xmlDeclare = xmlDataDoc.CreateXmlDeclaration("1.0", Nothing, Nothing)

                rootNode = xmlDataDoc.CreateElement(rootTag)

                xmlDataDoc.AppendChild(rootNode)

                xmlDataDoc.InsertBefore(xmlDeclare, rootNode)

            End If

 

            childElement = xmlDataDoc.CreateElement("Error")

            With childElement

                .SetAttribute("ID", XmlConvert.ToString(Guid.NewGuid))

                .SetAttribute("DateCreated", XmlConvert.ToString(Now, XmlDateTimeSerializationMode.Local))

                .SetAttribute("RemoteHostAddress", remoteHostAddress)

                .SetAttribute("Referer", referer)

                .SetAttribute("Message", excep.Message)

                .SetAttribute("Exception", excep.ToString())

            End With

 

            rootNode.AppendChild(childElement)

            xmlDataDoc.Save(filePathName)

 

        Catch exc As Exception

            Throw New Exception(exc.Message)

        Finally

            xmlDataDoc = Nothing

        End Try

    End Sub

Signature

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

 

Signature

I have written some useful ready to use functions in a Helper Class which can be used in XML string manipulation. These functions include following abilities :

1. Get XML data string from/to a class
2. Set/Remove CDATA in XML String
3. Clear All/Limited Special Characters from XML string
4. Set/Remove Root Node to XML String
5. Get Inner XML/Text on Relative Path
6. Get XML Document to/from an object
7. Get Custom Dataset from/to XML string

This Class project contains two classes. First class defines a class structure and second class uses this class for several functions.

Class : Helper

Option Explicit On

Option Strict On

Imports System.Xml.Serialization

Imports System.IO

Imports System.Text

Imports System.Xml

Imports System.Data

 

' ' The helper Class Transaction helper. This

' also includes the Exception class.

' ================================

' Version History:

' Date      Version     Author      Remarks

' 02/27/2007   1.0        Vishwa    Provides function to parse XML transaction

' ===================================

Public NotInheritable Class Helper

 

#Region "Private Constructor"

 

    Private Sub New()

 

    End Sub

 

#End Region

 

    Public Shared Function GetTransaction(ByVal strXML As String) As MyTransaction

        Dim xdcXML As New XmlDocument

        Dim xndXML As XmlNode

        Dim objMyTranasction As New MyTransaction

 

        Try

            strXML = ClearLimitedSpecialCharFromXML(strXML)

            xdcXML.LoadXml(strXML)

 

            If xdcXML.DocumentElement.Name = "MY_TRANSACTION" Then

                If Not xdcXML.DocumentElement("LOGIN") Is Nothing Then

                    xndXML = xdcXML.DocumentElement("LOGIN")

                    If Not xndXML.SelectSingleNode("TOKEN_ID") Is Nothing Then

                        objMyTranasction.TokenID = xndXML.SelectSingleNode("TOKEN_ID").InnerText()

                    End If

                    If Not xndXML.SelectSingleNode("USER_ID") Is Nothing Then

                        objMyTranasction.UserID = xndXML.SelectSingleNode("USER_ID").InnerText()

                    End If

                    If Not xndXML.SelectSingleNode("USER_PASSWORD") Is Nothing Then

                        objMyTranasction.UserPassword = xndXML.SelectSingleNode("USER_PASSWORD").InnerText()

                    End If

                    If Not xndXML.SelectSingleNode("IP_ADDRESS") Is Nothing Then

                        objMyTranasction.IPAddress = xndXML.SelectSingleNode("IP_ADDRESS").InnerText()

                    End If

 

 

                    If Not xdcXML.DocumentElement("TRANSACTION") Is Nothing Then

                        xndXML = xdcXML.DocumentElement("TRANSACTION")

                        If Not xndXML.SelectSingleNode("TRANSACTION_REFID") Is Nothing Then

                            objMyTranasction.TransactionRefID = xndXML.SelectSingleNode("TRANSACTION_REFID").InnerText

                        End If

 

                        If Not xndXML.SelectSingleNode("TRANSACTION_ID") Is Nothing Then

                            If IsNumeric(xndXML.SelectSingleNode("TRANSACTION_ID").InnerText) And _

                                    xndXML.SelectSingleNode("TRANSACTION_ID").InnerText.Trim.Length > 0 Then

                                objMyTranasction.TransactionID = CInt(xndXML.SelectSingleNode("TRANSACTION_ID").InnerText)

                            End If

                        End If

 

                        If Not xndXML.SelectSingleNode("TRANSACTION_REQUEST_DATE") Is Nothing Then

                            If IsDate(xndXML.SelectSingleNode("TRANSACTION_REQUEST_DATE").InnerText) And _

                                    xndXML.SelectSingleNode("TRANSACTION_REQUEST_DATE").InnerText.Trim.Length > 0 Then

                                objMyTranasction.TransactionRequestDate = _

                                        CDate(xndXML.SelectSingleNode("TRANSACTION_REQUEST_DATE").InnerText)

                            End If

                            If DateDiff(DateInterval.Day, objMyTranasction.TransactionRequestDate, Date.Today) <> 0 Then _

                                     objMyTranasction.TransactionRequestDate = DateAndTime.Now

                        Else

                            objMyTranasction.TransactionRequestDate = DateAndTime.Now

                        End If

 

                        If Not xndXML.SelectSingleNode("TRANSACTION_RESPONSE_DATE") Is Nothing Then

                            If IsDate(xndXML.SelectSingleNode("TRANSACTION_RESPONSE_DATE").InnerText) And _

                                    xndXML.SelectSingleNode("TRANSACTION_RESPONSE_DATE").InnerText.Trim.Length > 0 Then

                                objMyTranasction.TransactionResponseDate = _

                                        CDate(xndXML.SelectSingleNode("TRANSACTION_RESPONSE_DATE").InnerText)

                            End If

                            If DateDiff(DateInterval.Day, objMyTranasction.TransactionResponseDate, Date.Today) <> 0 Then _

                                         objMyTranasction.TransactionResponseDate = DateAndTime.Now

                        End If

 

 

                        If Not xndXML.SelectSingleNode("TRANSACTION_NAME") Is Nothing Then

                            objMyTranasction.WebMethodName = xndXML.SelectSingleNode("TRANSACTION_NAME").InnerText()

                        End If

 

                        If Not xndXML.SelectSingleNode("TRANSACTION_PARAMETERS") Is Nothing Then

                            objMyTranasction.WebMethodXML = xndXML.SelectSingleNode("TRANSACTION_PARAMETERS").InnerXml()

                        End If

 

                        If Not xndXML.SelectSingleNode("TRANSACTION_DATA") Is Nothing Then

                            objMyTranasction.WebMethodXMLData = xndXML.SelectSingleNode("TRANSACTION_DATA").InnerXml()

                        End If

 

 

                        If Not xndXML.SelectSingleNode("TRANSACTION_MESSAGE") Is Nothing Then

                            xndXML = xndXML.SelectSingleNode("TRANSACTION_MESSAGE")

 

                            If Not xndXML.SelectSingleNode("MESSAGE_NO") Is Nothing Then

                                If IsNumeric(xndXML.SelectSingleNode("MESSAGE_NO").InnerText) And _

                                        xndXML.SelectSingleNode("MESSAGE_NO").InnerText.Trim.Length > 0 Then

                                    objMyTranasction.MessageNumber = CInt(xndXML.SelectSingleNode("MESSAGE_NO").InnerText)

                                End If

                            End If

 

                            If Not xndXML.SelectSingleNode("MESSAGE_MODULE") Is Nothing Then

                                objMyTranasction.MessageModule = xndXML.SelectSingleNode("MESSAGE_MODULE").InnerText

                            End If

 

                            If Not xndXML.SelectSingleNode("MESSAGE_DETAIL") Is Nothing Then

                                objMyTranasction.MessageDetail = xndXML.SelectSingleNode("MESSAGE_DETAIL").InnerText

                            End If

                        End If

 

                        If objMyTranasction.TransactionRequestXML.Trim.Length = 0 Then _

                                objMyTranasction.TransactionRequestXML = strXML

 

                    End If

 

                End If

            End If

 

            Return objMyTranasction

        Catch exc As Exception

            Throw New Exception(exc.Message)

            Return objMyTranasction

        End Try

    End Function

 

 

    Public Shared Function PrepareTransactionXML(ByVal objMyTransaction As MyTransaction) As String

        Dim xdcXMLDoc As New XmlDocument

        Dim xndNode As XmlNode

        Dim xndChildNode As XmlNode

        Dim xndParentNode As XmlNode

        Dim xcdCData As XmlCDataSection

        Dim xdeDeclare As XmlDeclaration

 

        Try

            xdeDeclare = xdcXMLDoc.CreateXmlDeclaration("1.0", Nothing, Nothing)

            xndNode = xdcXMLDoc.CreateNode(XmlNodeType.Element, "MY_TRANSACTION", "")

            xdcXMLDoc.AppendChild(xndNode)

            xdcXMLDoc.InsertBefore(xdeDeclare, xndNode)

 

            xndChildNode = xdcXMLDoc.CreateNode(XmlNodeType.Element, "LOGIN", "")

            xndNode.AppendChild(xndChildNode)

 

            xndParentNode = xndChildNode

            xndChildNode = xdcXMLDoc.CreateNode(XmlNodeType.Element, "TOKEN_ID", "")

            xndChildNode.InnerText = objMyTransaction.TokenID

            xndParentNode.AppendChild(xndChildNode)

 

            xndChildNode = xdcXMLDoc.CreateNode(XmlNodeType.Element, "USER_ID", "")

            xndChildNode.InnerText = objMyTransaction.UserID

            xndParentNode.AppendChild(xndChildNode)

 

            xndChildNode = xdcXMLDoc.CreateNode(XmlNodeType.Element, "USER_PASSWORD", "")

            xndChildNode.InnerText = objMyTransaction.UserPassword

            xndParentNode.AppendChild(xndChildNode)

 

            xndChildNode = xdcXMLDoc.CreateNode(XmlNodeType.Element, "IP_ADDRESS", "")

            xndChildNode.InnerText = objMyTransaction.IPAddress

            xndParentNode.AppendChild(xndChildNode)

 

            xndChildNode = xdcXMLDoc.CreateNode(XmlNodeType.Element, "TRANSACTION", "")

            xndNode.AppendChild(xndChildNode)

 

            xndParentNode = xndChildNode

            xndChildNode = xdcXMLDoc.CreateNode(XmlNodeType.Element, "TRANSACTION_REFID", "")

            xndChildNode.InnerText = objMyTransaction.TransactionRefID

            xndParentNode.AppendChild(xndChildNode)

 

            xndChildNode = xdcXMLDoc.CreateNode(XmlNodeType.Element, "TRANSACTION_ID", "")

            xndChildNode.InnerText = objMyTransaction.TransactionID.ToString

            xndParentNode.AppendChild(xndChildNode)

 

            xndChildNode = xdcXMLDoc.CreateNode(XmlNodeType.Element, "TRANSACTION_REQUEST_DATE", "")

            xndChildNode.InnerText = objMyTransaction.TransactionRequestDate.ToString

            xndParentNode.AppendChild(xndChildNode)

 

            xndChildNode = xdcXMLDoc.CreateNode(XmlNodeType.Element, "TRANSACTION_RESPONSE_DATE", "")

            xndChildNode.InnerText = objMyTransaction.TransactionResponseDate.ToString 'DateTime.Now.ToString

            xndParentNode.AppendChild(xndChildNode)

 

            If objMyTransaction.WebMethodName.Trim.Length = 0 Then _

                objMyTransaction.WebMethodName = "TRANSACTION_METHOD_UNKNOWN"

            xndChildNode = xdcXMLDoc.CreateNode(XmlNodeType.Element, "TRANSACTION_NAME", "")

            xndChildNode.InnerText = objMyTransaction.WebMethodName

            xndParentNode.AppendChild(xndChildNode)

 

 

            xndChildNode = xdcXMLDoc.CreateNode(XmlNodeType.Element, "TRANSACTION_PARAMETERS", "")

            If objMyTransaction.WebMethodXML.Trim.Length > 0 And _

                    ValidateXMLString(objMyTransaction.WebMethodXML) Then

                xndChildNode.InnerText = objMyTransaction.WebMethodXML

                xndParentNode.AppendChild(xndChildNode)

            Else

                xndParentNode.AppendChild(xndChildNode)

                xcdCData = xdcXMLDoc.CreateCDataSection(objMyTransaction.WebMethodXML)

                xndChildNode.AppendChild(xcdCData)

            End If

 

 

            xndChildNode = xdcXMLDoc.CreateNode(XmlNodeType.Element, "TRANSACTION_DATA", "")

            If objMyTransaction.WebMethodXMLData.Trim.Length > 0 And _

                    ValidateXMLString(objMyTransaction.WebMethodXMLData) Then

                xndChildNode.InnerText = ClearAllSpecialCharFromXML(objMyTransaction.WebMethodXMLData)

                xndParentNode.AppendChild(xndChildNode)

            Else

                xndParentNode.AppendChild(xndChildNode)

                xcdCData = xdcXMLDoc.CreateCDataSection(objMyTransaction.WebMethodXMLData)

                xndChildNode.AppendChild(xcdCData)

            End If

 

 

            If objMyTransaction.MessageNumber > 0 Then

                xndChildNode = xdcXMLDoc.CreateNode(XmlNodeType.Element, "TRANSACTION_MESSAGE", "")

                xndParentNode.AppendChild(xndChildNode)

 

                xndParentNode = xndChildNode

 

                xndChildNode = xdcXMLDoc.CreateNode(XmlNodeType.Element, "MESSAGE_NO", "")

                xndChildNode.InnerText = objMyTransaction.MessageNumber.ToString

                xndParentNode.AppendChild(xndChildNode)

 

                xndChildNode = xdcXMLDoc.CreateNode(XmlNodeType.Element, "MESSAGE_MODULE", "")

                xndChildNode.InnerText = objMyTransaction.MessageModule

                xndParentNode.AppendChild(xndChildNode)

 

                xndChildNode = xdcXMLDoc.CreateNode(XmlNodeType.Element, "MESSAGE_DETAIL", "")

                If objMyTransaction.MessageDetail.Trim.Length > 0 And ValidateXMLString(objMyTransaction.MessageDetail) Then

                    xndChildNode.InnerText = ClearAllSpecialCharFromXML(objMyTransaction.MessageDetail)

                    xndParentNode.AppendChild(xndChildNode)

                Else

                    xndParentNode.AppendChild(xndChildNode)

                    xcdCData = xdcXMLDoc.CreateCDataSection(objMyTransaction.MessageDetail)

                    xndChildNode.AppendChild(xcdCData)

                End If

 

            End If

 

            Return ClearLimitedSpecialCharFromXML(xdcXMLDoc.OuterXml)

 

        Catch exc As Exception

            Throw New Exception(exc.Message)

        Finally

            xdcXMLDoc = Nothing

        End Try

    End Function

 

 

    Public Shared Function SetTransactionErrorMessage(ByVal objTransaction As MyTransaction, ByVal objError As ErrObject, ByVal objExc As Exception) As MyTransaction

        objTransaction.MessageNumber = objError.Number

        objTransaction.MessageModule = objExc.Source

        objTransaction.MessageDetail = objExc.ToString

        Return objTransaction

    End Function

 

 

    Public Shared Function ValidateXMLString(ByVal strXML As String) As Boolean

        Dim xdcXML As New XmlDocument

        Dim blnReturn As Boolean = False

 

        Try

            strXML = SetRootNodeToXML(strXML)

            xdcXML.LoadXml(strXML)

            blnReturn = True

        Catch

            blnReturn = False

        Finally

            ValidateXMLString = blnReturn

        End Try

 

    End Function

 

 

    Public Shared Function SetRootNodeToXML(ByVal strXML As String) As String

        Return "<ROOT>" & strXML & "</ROOT>"

    End Function

 

 

    Public Shared Function RemoveRootNodeFromXML(ByVal strXML As String) As String

        Return Replace(Replace(strXML, "<ROOT>", ""), "</ROOT>", "")

    End Function

 

 

    Public Shared Function RemoveCDATAFromXML(ByVal strXML As String) As String

        If Left(strXML, 9) = "<![CDATA[" And Right(strXML, 3) = "]]>" Then

            RemoveCDATAFromXML = Mid(strXML, 10, strXML.Trim.Length - 12)

        Else

            RemoveCDATAFromXML = strXML

        End If

    End Function

 

 

    Public Shared Function SetCDATAToXML(ByVal strXML As String) As String

        Return "<![CDATA[" & strXML & "]]>"

    End Function

 

 

    Public Shared Function ClearAllSpecialCharFromXML(ByVal strXML As String) As String

        '.Net 2.0 Creates xml:space=""preserve" in dataset if data is blank

        Return Replace(Replace(Replace(Replace(Replace(Replace(strXML, "&amp;", "&"), "&apos;", "'"), "&quot;", """"), "&lt;", "<"), "&gt;", ">"), " xml:space=""preserve""", "")

    End Function

 

 

    Public Shared Function ClearLimitedSpecialCharFromXML(ByVal strXML As String) As String

        '.Net 2.0 Creates xml:space=""preserve" in dataset if data is blank

        Return Replace(Replace(Replace(Replace(strXML, "&lt;", "<"), "&gt;", ">"), "&amp;nbsp;", "&nbsp;"), " xml:space=""preserve""", "")

    End Function

 

 

    Public Shared Function GetInnerXMLOnRelativePath(ByVal strXML As String, ByVal strRelativePath As String) As String

        Dim xdcXML As New XmlDocument

        Dim strInnerXML As String = ""

 

        Try

            xdcXML.LoadXml(strXML)

            strInnerXML = xdcXML.SelectSingleNode(strRelativePath).InnerXml.ToString

        Catch

            strInnerXML = ""

        Finally

            GetInnerXMLOnRelativePath = strInnerXML

        End Try

    End Function

 

 

    Public Shared Function GetInnerTextOnRelativePath(ByVal strXML As String, ByVal strRelativePath As String) As String

        Dim xdcXML As New XmlDocument

        Dim strInnerText As String = ""

 

        Try

            xdcXML.LoadXml(strXML)

            strInnerText = xdcXML.SelectSingleNode(strRelativePath).InnerText.ToString

        Catch

            strInnerText = ""

        Finally

            GetInnerTextOnRelativePath = strInnerText

        End Try

    End Function

 

 

    Public Shared Function GetObjectAsXmlDocument(ByVal objObject As Object) As XmlDocument

        Dim xdcXML As New XmlDocument 'Convert the obj into an Xml document

        Dim dstXML As DataSet = New DataSet

 

        Try

            Dim xsrXML As XmlSerializer = New XmlSerializer(objObject.GetType())

            Dim sbrXML As StringBuilder = New StringBuilder

            Dim twrXML As TextWriter = New StringWriter(sbrXML)

            xsrXML.Serialize(twrXML, objObject)

            Dim srdReader As StringReader = New StringReader(twrXML.ToString())

 

            'Read the Xml from the StringReader object

            dstXML.ReadXml(srdReader)

            xdcXML.LoadXml(dstXML.GetXml())

            Return xdcXML

        Catch

            Throw

        End Try

    End Function

 

 

    Public Shared Function GetXMLFromDataSet(ByVal strWebMethodName As String, _

                   ByVal dstDataset As DataSet) As String

        Dim dtbFindTable As DataTable

        Dim dclTable As DataColumn

        Dim intCount As Integer = 1

        Dim strXML As String

 

        If dstDataset.Tables.Count > 0 Then

            For Each dtbFindTable In dstDataset.Tables

                If dstDataset.Tables.Count = 1 Then

                    dtbFindTable.TableName = strWebMethodName & "_TABLE"

                    For Each dclTable In dstDataset.Tables(0).Columns

                        dclTable.ColumnName = UCase(dclTable.ColumnName)

                    Next

                Else

                    dtbFindTable.TableName = strWebMethodName & "_TABLE" & intCount

                    For Each dclTable In dstDataset.Tables(intCount - 1).Columns

                        dclTable.ColumnName = UCase(dclTable.ColumnName)

                    Next

                    intCount = intCount + 1

                End If

            Next

            dstDataset.DataSetName = strWebMethodName & "_DATASET"

            strXML = dstDataset.GetXml

        Else

            strXML = ""

        End If

        GetXMLFromDataSet = ClearLimitedSpecialCharFromXML(strXML)

 

    End Function

 

 

    Public Shared Function GetObjectAsDataSet(ByVal objObject As Object) As DataSet

        Dim xdcXML As New XmlDocument 'Convert the obj into an Xml document

        Dim dstXML As DataSet = New DataSet

        Try

            Dim xsrXML As XmlSerializer = New XmlSerializer(objObject.GetType())

            Dim sbrXML As StringBuilder = New StringBuilder

            Dim twrXML As TextWriter = New StringWriter(sbrXML)

            xsrXML.Serialize(twrXML, objObject)

            Dim srdReader As StringReader = New StringReader(twrXML.ToString())

            'Read the Xml from the StringReader object

            dstXML.ReadXml(srdReader)

            xdcXML.LoadXml(dstXML.GetXml())

            Return dstXML

        Catch

            Throw

        End Try

    End Function

 

 

    Public Shared Function GetDataSetFromXMLString(ByVal strXML As String) As DataSet

        Dim dstXML As DataSet = New DataSet

        Try

            Dim srdXML As StringReader = New StringReader(strXML)

            dstXML.ReadXml(srdXML, XmlReadMode.Auto)

            Return dstXML

        Catch

            Throw

        End Try

    End Function

 

End Class

 

 

 

Class : MyTransaction

 

Option Explicit On

Option Strict On

    Public NotInheritable Class MyTransaction

        Private strTokenID As String

        Private strUserID As String

        Private strPassword As String

        Private strIPAddress As String

        Private intTransactionID As Int32

        Private strTransactionRefID As String

        Private dtmTransactionReqDate As DateTime

        Private dtmTransactionResDate As DateTime

        Private strTransactionReqXML As String

        Private strTransactionResXML As String

        Private strWebMethodName As String

        Private strWebMethodXML As String

        Private strWebMethodXMLData As String

        Private intMessageNo As Int32

        Private strMessageModule As String

        Private strMessageDetail As String

#Region "Private Constructor"

 

        Sub New()

 

        End Sub

 

#End Region

        Public Property TokenID() As String

            Get

                If IsNothing(strTokenID) Then strTokenID = ""

                Return strTokenID

            End Get

            Set(ByVal Value As String)

                strTokenID = Value

            End Set

        End Property

 

        Public Property UserID() As String

            Get

                If IsNothing(strUserID) Then strUserID = ""

                Return strUserID

            End Get

            Set(ByVal Value As String)

                strUserID = Value

            End Set

        End Property

        Public Property UserPassword() As String

            Get

                If IsNothing(strPassword) Then strPassword = ""

                Return strPassword

            End Get

            Set(ByVal Value As String)

                strPassword = Value

            End Set

        End Property

 

        Public Property IPAddress() As String

            Get

                If IsNothing(strIPAddress) Then strIPAddress = ""

                Return strIPAddress

            End Get

            Set(ByVal Value As String)

                strIPAddress = Value

            End Set

        End Property

        Public Property TransactionID() As Int32

            Get

                Return intTransactionID

            End Get

            Set(ByVal Value As Int32)

                intTransactionID = Value

            End Set

        End Property

        Public Property TransactionRefID() As String

            Get

                If IsNothing(strTransactionRefID) Then strTransactionRefID = ""

                Return strTransactionRefID

            End Get

            Set(ByVal Value As String)

                strTransactionRefID = Value

            End Set

        End Property

 

 

        Public Property TransactionRequestDate() As Date

            Get

                If DateDiff(DateInterval.Day, dtmTransactionReqDate, DateAndTime.Now) <> 0 Then dtmTransactionReqDate = DateAndTime.Now

                Return dtmTransactionReqDate

            End Get

            Set(ByVal dtmValue As Date)

                dtmTransactionReqDate = dtmValue

            End Set

        End Property

        Public Property TransactionResponseDate() As Date

            Get

                If DateDiff(DateInterval.Second, dtmTransactionResDate, DateAndTime.Now) <> 0 Then dtmTransactionResDate = DateAndTime.Now

                Return dtmTransactionResDate

            End Get

            Set(ByVal dtmValue As Date)

                dtmTransactionResDate = dtmValue

            End Set

        End Property

        Public Property TransactionRequestXML() As String

            Get

                If IsNothing(strTransactionReqXML) Then strTransactionReqXML = ""

                Return strTransactionReqXML

            End Get

            Set(ByVal Value As String)

                strTransactionReqXML = Value

            End Set

        End Property

        Public Property TransactionResponseXML() As String

            Get

                If IsNothing(strTransactionResXML) Then strTransactionResXML = ""

                Return strTransactionResXML

            End Get

            Set(ByVal Value As String)

                strTransactionResXML = Value

            End Set

        End Property

        Public Property WebMethodName() As String

            Get

                If IsNothing(strWebMethodName) Then strWebMethodName = ""

                Return strWebMethodName

            End Get

            Set(ByVal Value As String)

                strWebMethodName = Value

            End Set

        End Property

        Public Property WebMethodXML() As String

            Get

                If IsNothing(strWebMethodXML) Then strWebMethodXML = ""

                Return Helper.RemoveCDATAFromXML(strWebMethodXML)

            End Get

            Set(ByVal Value As String)

                strWebMethodXML = Value

            End Set

        End Property

        Public Property WebMethodXMLData() As String

            Get

                If IsNothing(strWebMethodXMLData) Then strWebMethodXMLData = ""

                Return Helper.RemoveCDATAFromXML(strWebMethodXMLData)

            End Get

            Set(ByVal Value As String)

                strWebMethodXMLData = Value

            End Set

        End Property

        Public Property MessageNumber() As Int32

            Get

                Return intMessageNo

            End Get

            Set(ByVal Value As Int32)

                intMessageNo = Value

            End Set

        End Property

        Public Property MessageModule() As String

            Get

                If IsNothing(strMessageModule) Then strMessageModule = ""

                Return strMessageModule

            End Get

            Set(ByVal Value As String)

                strMessageModule = Value

            End Set

        End Property

        Public Property MessageDetail() As String

            Get

                If IsNothing(strMessageDetail) Then strMessageDetail = ""

                Return Helper.RemoveCDATAFromXML(strMessageDetail)

            End Get

            Set(ByVal Value As String)

                strMessageDetail = Value

            End Set

        End Property

    End Class

 

 

Signature

About Me

Me Hello,my name is Vishwa Mohan Kumar.
I am a Software Architect. This blog is result of my experiments.

Flickr Photos

Calendar

<<  September 2010  >>
MoTuWeThFrSaSu
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910

View posts in large calendar

Recent Comments

Comment RSS

Live Traffic Feed