Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.Xml
Imports Microsoft.VisualBasic
<WebService(Namespace:="ws")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Public Class ListManager
Inherits System.Web.Services.WebService
Dim XmlPath As String = "list.xml"
Dim listDoc As New XmlDocument
Dim node As XmlNode
Dim strList As String = ""
Dim nms As XmlNodeList
<WebMethod()> Public Sub SetList(ByVal cbo As String, ByVal NameList As String)
Dim NameNode = Nothing
Dim node As XmlNode
Dim nms As XmlNodeList = Nothing
Dim listDoc As XmlDocument = New XmlDocument()
Dim ws As XmlWhitespace = listDoc.CreateWhitespace(ControlChars.CrLf)
Dim tb As XmlWhitespace = listDoc.CreateWhitespace(ControlChars.Tab)
On Error GoTo ErrResponse
listDoc.Load(Server.MapPath(XmlPath))
Select Case cbo
Case "Name"
nms = listDoc.SelectNodes("/List/Team/Name")
NameNode = listDoc.SelectSingleNode("/List/Team")
Case "Reason"
nms = listDoc.SelectNodes("/List/Reasons/Reason")
NameNode = listDoc.SelectSingleNode("/List/Reasons")
Case "Type"
nms = listDoc.SelectNodes("/List/Types/Type")
NameNode = listDoc.SelectSingleNode("/List/Types")
End Select
Dim i As Integer
Assign the string of comma delimited values to an array
Dim listToTxt() As String = Split(NameList, ",")
For i = 0 To UBound(listToTxt)
If Item is NOT found in the collection of nodes then add it
If IsDupe(nms, listToTxt(i)) = False Then
node = listDoc.CreateNode(XmlNodeType.Element, "", cbo, "")
node.InnerText = listToTxt(i)
NameNode.AppendChild(node)
Indent the new node
NameNode.InsertBefore(tb, node)
Insert a carriage return after the new node
NameNode.InsertAfter(ws, node)
End If
Next
listDoc.Save(Server.MapPath(XmlPath))
ErrResponse:
If Err.Number > 0 Then
Resume Next
End If
End Sub
<WebMethod()> Public Function GetList(ByVal cbo As String) As String
On Error GoTo ErrResponse
listDoc.Load(Server.MapPath(XmlPath))
Select Case cbo
Case "Name"
nms = listDoc.SelectNodes("/List/Team/Name")
Case "Reason"
nms = listDoc.SelectNodes("/List/Reasons/Reason")
Case "Type"
nms = listDoc.SelectNodes("/List/Types/Type")
End Select
Loop through the nodes and add the names to the list
For Each node In nms
If Len(strList) = 0 Then
strList += node.InnerText
Else
strList += "," & node.InnerText
End If
Next
Return strList
ErrResponse:
If Err.Number > 0 Then
Resume Next
End If
End Function
<WebMethod()> Public Sub RemoveEntry(ByVal cbo As String, ByVal nme As String)
On Error GoTo ErrResponse
Dim rmNode, srchNode As System.Xml.XmlNode
listDoc.Load(Server.MapPath(XmlPath))
Select the node to search
srchNode = listDoc.SelectSingleNode("/List/" & cbo)
Loop through the children nodes and find the one to remove
For Each rmNode In srchNode.ChildNodes
If rmNode.InnerText = nme Then
Remove the specified node
srchNode.RemoveChild(rmNode)
Save the file
listDoc.Save(Server.MapPath(XmlPath))
Exit For
End If
Next
ErrResponse:
If Err.Number > 0 Then
Resume Next
End If
End Sub
Private Function IsDupe(ByVal ns As XmlNodeList, ByVal strEntry As String) As Boolean
Dim node As XmlNode
For Each node In ns
If node.InnerText = strEntry Then
Return True
Exit Function
End If
Next
Return False
End Function
End Class