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
Code Snippet
- 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, "&", "&"), "'", "'"), """, """"), "<", "<"), ">", ">"), " 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, "<", "<"), ">", ">"), "&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
Code Snippet
- 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
No comments:
Post a Comment