Imports System.Xml
Public Class Oxml
Private m_XmlDoc As XmlDocument
Private m_xmlWriter As XmlTextWriter
Private m_XmlPath As String
Private m_hshWE As Hashtable
Private m_KeyWE As String
Private m_strDE() As String
Private m_hshUE As Hashtable
Private m_StartElement As String
Private m_SelectionNode As String
'Constructor
Public Sub New()
End Sub
'Constructor
Public Sub New(ByVal xmlpath As String, ByVal startelement As String, ByVal hshWE As Hashtable, _
ByVal keyWE As String, ByVal strDE() As String, ByVal hshUE As Hashtable, ByVal selectionnodes As String)
m_XmlDoc = New XmlDocument()
m_XmlPath = xmlpath
m_hshWE = hshWE
m_KeyWE = keyWE
m_strDE = strDE
m_hshUE = hshUE
m_SelectionNode = selectionnodes
End Sub
'Constructor
Public Sub New(ByVal xmlpath As String, ByVal startelement As String, ByVal hshWE As Hashtable, _
ByVal keyWE As String, ByVal selectionnode As String)
m_XmlDoc = New XmlDocument()
m_XmlPath = xmlpath
m_StartElement = startelement
m_hshWE = hshWE
m_KeyWE = keyWE
m_SelectionNode = selectionnode
End Sub
'Check the path of the XML file
Private Function IsXMLPathValid() As Boolean
If IO.File.Exists(m_XmlPath) = True Then
Return True
Else
Return False
End If
End Function
'Write Element
Public Sub WriteElement()
If m_KeyWE = String.Empty Then
MsgBox("Key Write Element Invalid!")
Return
End If
If m_hshWE Is Nothing Then
MsgBox("Write Elements Invalid!")
Return
End If
Dim strMainIdentifier As String = m_hshWE.Item(m_KeyWE).ToString
If IsXMLValid() = True AndAlso HasElement(strMainIdentifier) = False Then
If IsXMLPathValid() = False Then
MsgBox("XML File Invalid!")
Else
m_XmlDoc.Load(m_XmlPath)
Dim root As XmlNode = m_XmlDoc.DocumentElement
Dim xmlG As XmlElement = m_XmlDoc.CreateElement("Group")
'xmlG.InnerText = String.Empty
For Each de As DictionaryEntry In m_hshWE
Dim xmlE As XmlElement = m_XmlDoc.CreateElement(de.Key)
xmlE.InnerText = de.Value
xmlG.AppendChild(xmlE)
'root.AppendChild(xmlE)
root.AppendChild(xmlG)
m_XmlDoc.Save(m_XmlPath)
Next
'root.AppendChild(m_XmlDoc.CreateNode(XmlNodeType.EndElement, "/Group", Nothing))
End If
End If
If IsXMLValid() = False AndAlso HasElement(strMainIdentifier) = False Then
If m_StartElement = String.Empty Then
MsgBox("Start Element Invalid!")
Else
m_xmlWriter = New XmlTextWriter(m_XmlPath, Nothing) 'Application.StartupPath & "\InternalXML\URLCat.XML", Nothing)
With m_xmlWriter
.Indentation = 4
.IndentChar = " "c
.Formatting = Formatting.Indented
.WriteStartDocument()
.WriteStartElement(m_StartElement)
.WriteStartElement("Group")
For Each de As DictionaryEntry In m_hshWE
.WriteElementString(de.Key, de.Value)
Next
.WriteEndElement()
.WriteEndElement()
.WriteEndDocument()
.Flush()
.Close()
End With
End If
End If
End Sub
'Delete the element
Public Sub DeleteElement()
If IsXMLPathValid() = False Then
MsgBox("XML File Invalid!")
Else
m_XmlDoc.Load(m_XmlPath)
For Each s As String In Me.m_strDE
For Each xmlnd As XmlNode In m_XmlDoc.SelectSingleNode(m_SelectionNode)
If String.Compare(xmlnd.InnerText, s, True) = 0 Then
'xmlnd.SelectSingleNode("/URLCategories/Category").ParentNode.RemoveAll()
xmlnd.ParentNode.RemoveChild(xmlnd)
End If
Next
m_XmlDoc.Save(m_XmlPath)
Next
End If
End Sub
Public Sub UpdateElement(ByVal elementname As String)
End Sub
'Check to see if the XML document is valid
Public Function IsXMLValid() As Boolean
Dim xmlDoc As New XmlDocument()
Try
xmlDoc.Load(m_XmlPath)
Catch ex As Exception
MsgBox(ex.Tostring)
Return False
End Try
Return True
End Function
'Check to see if the XML has that element
Public Function HasElement(ByVal elementname As String) As Boolean
Dim blnHasElement As Boolean
Try
If IO.File.Exists(m_XmlPath) Then
Dim xmlDoc As New XmlDocument()
xmlDoc.Load(m_XmlPath)
For Each xmlnd As XmlNode In xmlDoc.SelectNodes(m_SelectionNode)
If String.Compare(xmlnd.InnerText, elementname, True) = 0 Then
blnHasElement = True
Exit For
End If
Next
End If
Catch ex As Exception
Return False
End Try
Return blnHasElement
End Function
End Class

No comments:
Post a Comment