Option Explicit On Option Strict On Module XMLGeneric Public Sub AddAttribute(ByVal Elm As System.Xml.XmlElement, ByVal Name As String, ByVal Value As String) Dim Attr As System.Xml.XmlAttribute = Elm.OwnerDocument.CreateAttribute(Name) Attr.Value = Value Elm.Attributes.Append(Attr) End Sub Public Sub AddAttribute(ByVal Elm As System.Xml.XmlElement, ByVal Name As String, ByVal Value As Object) Dim Attr As System.Xml.XmlAttribute = Elm.OwnerDocument.CreateAttribute(Name) If Value Is Nothing Then Attr.Value = "" Else Attr.Value = Value.ToString End If Elm.Attributes.Append(Attr) End Sub Public Function AddElement(ByVal Elm As System.Xml.XmlElement, ByVal Name As String) As System.Xml.XmlElement Dim E As System.Xml.XmlElement = Elm.OwnerDocument.CreateElement(Name) Elm.AppendChild(E) Return E End Function Public Function AddValueElement(ByVal Elm As System.Xml.XmlElement, ByVal Name As String, ByVal Value As String) As System.Xml.XmlElement Dim E As System.Xml.XmlElement = Elm.OwnerDocument.CreateElement(Name) Elm.AppendChild(E) E.InnerText = Value Return E End Function 'Private Sub OutputXML(ByVal XmlDoc As System.Xml.XmlDataDocument) ' Dim S As New System.Xml.XmlWriterSettings() ' S.Indent = True ' S.IndentChars = " " ' S.NewLineChars = vbCrLf ' S.NewLineHandling = System.Xml.NewLineHandling.Replace ' S.Encoding = Response.ContentEncoding ' Dim W As System.Xml.XmlWriter = System.Xml.XmlWriter.Create(Response.OutputStream, S) ' XmlDoc.Save(W) ' W.Close() 'End Sub Public Sub SaveXML(ByVal XmlDoc As System.Xml.XmlDataDocument, FilePathName As String) Dim S As New System.Xml.XmlWriterSettings() S.Indent = True S.IndentChars = " " S.NewLineChars = vbCrLf S.NewLineHandling = System.Xml.NewLineHandling.Replace 'S.Encoding = System.Text.ASCIIEncoding.ASCII S.Encoding = System.Text.UTF8Encoding.UTF8 Dim W As System.Xml.XmlWriter = System.Xml.XmlWriter.Create(FilePathName, S) XmlDoc.Save(W) W.Close() End Sub Public Sub SaveXML(ByVal XmlDoc As System.Xml.XmlDataDocument, Stm As System.IO.Stream) Dim S As New System.Xml.XmlWriterSettings() S.Indent = True S.IndentChars = " " S.NewLineChars = vbCrLf S.NewLineHandling = System.Xml.NewLineHandling.Replace 'S.Encoding = System.Text.ASCIIEncoding.ASCII S.Encoding = System.Text.UTF8Encoding.UTF8 Dim W As System.Xml.XmlWriter = System.Xml.XmlWriter.Create(Stm, S) XmlDoc.Save(W) W.Close() End Sub Public Function XMLNodeTryGetAttr(N As System.Xml.XmlNode, AttrName As String, ByRef S As String) As Boolean S = Nothing Dim XMLAttr As System.Xml.XmlAttribute = N.Attributes(AttrName) If XMLAttr Is Nothing Then Return False S = XMLAttr.Value Return True End Function Public Function GetValFromXPath(ByVal nav As System.Xml.XPath.XPathNavigator, ByVal XPath As String) As String If XPath.Length = 0 Then Return "" ' no go End If ' try to resolve this one to a value Try Dim o As Object = nav.Evaluate(XPath) If TypeOf o Is String Then Return CStr(o) ElseIf TypeOf o Is Boolean Then Return CStr(o) ElseIf TypeOf o Is Double Then Return CStr(o) ElseIf TypeOf o Is System.Xml.XPath.XPathNodeIterator Then For Each XPN As System.Xml.XPath.XPathNavigator In CType(o, System.Xml.XPath.XPathNodeIterator) If XPN.Value <> "" Then Return XPN.Value Next Else Debug.Print(" !GetValFromXPath: Unexpected Type: {0}", o.GetType.ToString) End If Catch ex As Exception Debug.Print(" !GetValFromXPath: Exception: {0}", ex.Message + vbCrLf + ex.StackTrace) End Try ' should not be here Return "" End Function Public Function GetValFromXPath(ByVal nav As System.Xml.XPath.XPathNavigator, ByVal XPath1 As String, ByVal XPath2 As String) As String Dim XPathVal As String XPathVal = GetValFromXPath(nav, XPath1) If XPathVal = "" Then XPathVal = GetValFromXPath(nav, XPath2) ' try again End If Return XPathVal End Function Public Function GetNodeFromXPath(ByVal nav As System.Xml.XPath.XPathNavigator, ByVal XPath As String) As System.Xml.XPath.XPathNavigator If XPath.Length = 0 Then Return Nothing ' no go End If ' try to resolve this one to a value Try Return nav.SelectSingleNode(XPath) Catch ex As Exception Debug.Print(" !GetNodeFromXPath: Exception: {0}", ex.Message + vbCrLf + ex.StackTrace) End Try ' should not be here Return Nothing End Function Public Function GetNodeFromXPath(ByVal nav As System.Xml.XPath.XPathNavigator, ByVal XPath As Xml.XPath.XPathExpression) As System.Xml.XPath.XPathNavigator If XPath Is Nothing Then Return Nothing ' no go End If ' try to resolve this one to a value Try Return nav.SelectSingleNode(XPath) Catch ex As Exception Debug.Print(" !GetNodeFromXPath: Exception: {0}", ex.Message + vbCrLf + ex.StackTrace) End Try ' should not be here Return Nothing End Function End Module