Richard Kreidl
asked on
How to search and delete elements in a XML file.
I want to be able to delete and search for elements in a XML file, I'm using the code below for adding elements which works great:
Public Sub cmdAddElement_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAddElement.Click
Dim doc As New XmlDocument
doc.Load("c:UMZ.xml")
Dim root As XmlNode = doc.DocumentElement
Dim EleName As XmlElement = doc.CreateElement("Name")
Dim ElePreMVS As XmlElement = doc.CreateElement("PreMVS" )
Dim ElePostMVS As XmlElement = doc.CreateElement("PostMVS ")
Dim ElePreCS As XmlElement = doc.CreateElement("PreCS")
Dim ElePostCS As XmlElement = doc.CreateElement("PostCS" )
Dim EleMisc As XmlElement = doc.CreateElement("Misc")
EleName.InnerText = txtName.Text
ElePreMVS.InnerText = txtPreMVS.Text
ElePostMVS.InnerText = txtPostMVS.Text
ElePreCS.InnerText = txtPreCS.Text
ElePostCS.InnerText = txtPostCS.Text
EleMisc.InnerText = txtMisc.Text
root.AppendChild(EleName)
root.AppendChild(ElePreMVS )
root.AppendChild(ElePostMV S)
root.AppendChild(ElePreCS)
root.AppendChild(ElePostCS )
root.AppendChild(EleMisc)
doc.Save("c:UMZ.xml")
End Sub
So, basically the XML file could dynamically grow or shrink with elements as they're added or deleted.
Here is the XML file with one element row:
<?xml version="1.0" standalone="yes"?>
<UMZ>
<Name>some text</Name>
<PreMVS>some text</PreMVS>
<PostMVS>some text</PostMVS>
<PreCS>some text</PreCS>
<PostCS>some text</PostCS>
<Misc>some text</Misc>
</UMZ>
Here is a blank XML file without any rows:
<?xml version="1.0" standalone="yes"?>
<UMZ>
</UMZ>
Here is the XML file with two element rows:
<?xml version="1.0" standalone="yes"?>
<UMZ>
<Name>some text</Name>
<PreMVS>some text</PreMVS>
<PostMVS>some text</PostMVS>
<PreCS>some text</PreCS>
<PostCS>some text</PostCS>
<Misc>some text</Misc>
<Name>some more text</Name>
<PreMVS>some more text</PreMVS>
<PostMVS>some more text</PostMVS>
<PreCS>some more text</PreCS>
<PostCS>some more text</PostCS>
<Misc>some more text</Misc>
</UMZ>
Then I would need another button that would search for a 'Name' of a element in the XML file that I would specify and populate the cooresponding textboxes. Then I could delete the element from the XML if so desired with a delete button.
Public Sub cmdAddElement_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAddElement.Click
Dim doc As New XmlDocument
doc.Load("c:UMZ.xml")
Dim root As XmlNode = doc.DocumentElement
Dim EleName As XmlElement = doc.CreateElement("Name")
Dim ElePreMVS As XmlElement = doc.CreateElement("PreMVS"
Dim ElePostMVS As XmlElement = doc.CreateElement("PostMVS
Dim ElePreCS As XmlElement = doc.CreateElement("PreCS")
Dim ElePostCS As XmlElement = doc.CreateElement("PostCS"
Dim EleMisc As XmlElement = doc.CreateElement("Misc")
EleName.InnerText = txtName.Text
ElePreMVS.InnerText = txtPreMVS.Text
ElePostMVS.InnerText = txtPostMVS.Text
ElePreCS.InnerText = txtPreCS.Text
ElePostCS.InnerText = txtPostCS.Text
EleMisc.InnerText = txtMisc.Text
root.AppendChild(EleName)
root.AppendChild(ElePreMVS
root.AppendChild(ElePostMV
root.AppendChild(ElePreCS)
root.AppendChild(ElePostCS
root.AppendChild(EleMisc)
doc.Save("c:UMZ.xml")
End Sub
So, basically the XML file could dynamically grow or shrink with elements as they're added or deleted.
Here is the XML file with one element row:
<?xml version="1.0" standalone="yes"?>
<UMZ>
<Name>some text</Name>
<PreMVS>some text</PreMVS>
<PostMVS>some text</PostMVS>
<PreCS>some text</PreCS>
<PostCS>some text</PostCS>
<Misc>some text</Misc>
</UMZ>
Here is a blank XML file without any rows:
<?xml version="1.0" standalone="yes"?>
<UMZ>
</UMZ>
Here is the XML file with two element rows:
<?xml version="1.0" standalone="yes"?>
<UMZ>
<Name>some text</Name>
<PreMVS>some text</PreMVS>
<PostMVS>some text</PostMVS>
<PreCS>some text</PreCS>
<PostCS>some text</PostCS>
<Misc>some text</Misc>
<Name>some more text</Name>
<PreMVS>some more text</PreMVS>
<PostMVS>some more text</PostMVS>
<PreCS>some more text</PreCS>
<PostCS>some more text</PostCS>
<Misc>some more text</Misc>
</UMZ>
Then I would need another button that would search for a 'Name' of a element in the XML file that I would specify and populate the cooresponding textboxes. Then I could delete the element from the XML if so desired with a delete button.
ASKER
I guess I could layout the XML file this way if it makes it easier:
<?xml version="1.0" standalone="yes"?>
<UMZ>
<Server name="some text">
<PreMVS>some text</PreMVS>
<PostMVS>some text</PostMVS>
<PreCS>some text</PreCS>
<PostCS>some text</PostCS>
<Misc>some text</Misc>
</Server>
<Server name="some more text">
<PreMVS>some more text</PreMVS>
<PostMVS>some more text</PostMVS>
<PreCS>some more text</PreCS>
<PostCS>some more text</PostCS>
<Misc>some more text</Misc>
</Server>
</UMZ>
<?xml version="1.0" standalone="yes"?>
<UMZ>
<Server name="some text">
<PreMVS>some text</PreMVS>
<PostMVS>some text</PostMVS>
<PreCS>some text</PreCS>
<PostCS>some text</PostCS>
<Misc>some text</Misc>
</Server>
<Server name="some more text">
<PreMVS>some more text</PreMVS>
<PostMVS>some more text</PostMVS>
<PreCS>some more text</PreCS>
<PostCS>some more text</PostCS>
<Misc>some more text</Misc>
</Server>
</UMZ>
Change the filename back to your version... and this is using the format as you last mentioned:
UMZ
Server
Properties
========================== ====
Function LoadServer(ByVal desiredServer As String) As Boolean
Dim doc As New XmlDocument
doc.Load("D:\My Documents\umz.xml")
Dim root As XmlNode = doc.DocumentElement
Dim nodeServer As XmlNode
Dim thisServerName As String
Dim nodeFound As Boolean = False
For Each nodeServer In root.ChildNodes
Try
thisServerName = nodeServer.Attributes("nam e").InnerT ext
If thisServerName.ToLower = desiredServer.ToLower Then
'IF YOU'RE LOADING
txtName.Text = thisServerName
txtPreMVS.Text = nodeServer.SelectSingleNod e("PreMVS" ).InnerTex t
txtPostMVS.Text = nodeServer.SelectSingleNod e("PostMVS ").InnerTe xt
txtPreCS.Text = nodeServer.SelectSingleNod e("PreCS") .InnerText
txtPostCS.Text = nodeServer.SelectSingleNod e("PostCS" ).InnerTex t
txtMisc.Text = nodeServer.SelectSingleNod e("Misc"). InnerText
nodeFound = True
Exit For
End If
Catch ex As Exception
End Try
Next
Return nodeFound
End Function
Function DeleteServer(ByVal desiredServer As String) As Boolean
Dim doc As New XmlDocument
doc.Load("D:\My Documents\umz.xml")
Dim root As XmlNode = doc.DocumentElement
Dim nodeServer As XmlNode
Dim thisServerName As String
Dim nodeFound As Boolean = False
For Each nodeServer In root.ChildNodes
Try
thisServerName = nodeServer.Attributes("nam e").InnerT ext
If thisServerName.ToLower = desiredServer.ToLower Then
nodeFound = True
Exit For
End If
Catch ex As Exception
End Try
Next
If nodeFound Then
root.RemoveChild(nodeServe r)
doc.Save("D:\My Documents\umz.xml")
End If
Return nodeFound
End Function
UMZ
Server
Properties
==========================
Function LoadServer(ByVal desiredServer As String) As Boolean
Dim doc As New XmlDocument
doc.Load("D:\My Documents\umz.xml")
Dim root As XmlNode = doc.DocumentElement
Dim nodeServer As XmlNode
Dim thisServerName As String
Dim nodeFound As Boolean = False
For Each nodeServer In root.ChildNodes
Try
thisServerName = nodeServer.Attributes("nam
If thisServerName.ToLower = desiredServer.ToLower Then
'IF YOU'RE LOADING
txtName.Text = thisServerName
txtPreMVS.Text = nodeServer.SelectSingleNod
txtPostMVS.Text = nodeServer.SelectSingleNod
txtPreCS.Text = nodeServer.SelectSingleNod
txtPostCS.Text = nodeServer.SelectSingleNod
txtMisc.Text = nodeServer.SelectSingleNod
nodeFound = True
Exit For
End If
Catch ex As Exception
End Try
Next
Return nodeFound
End Function
Function DeleteServer(ByVal desiredServer As String) As Boolean
Dim doc As New XmlDocument
doc.Load("D:\My Documents\umz.xml")
Dim root As XmlNode = doc.DocumentElement
Dim nodeServer As XmlNode
Dim thisServerName As String
Dim nodeFound As Boolean = False
For Each nodeServer In root.ChildNodes
Try
thisServerName = nodeServer.Attributes("nam
If thisServerName.ToLower = desiredServer.ToLower Then
nodeFound = True
Exit For
End If
Catch ex As Exception
End Try
Next
If nodeFound Then
root.RemoveChild(nodeServe
doc.Save("D:\My Documents\umz.xml")
End If
Return nodeFound
End Function
ASKER
I sort of get what your saying here, but how to I add a server to the XML file. The code you've given me is for doing a search and then deleting the server, right??
ASKER
Here is the code I have so far:
Private Sub cmdAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAdd.Click
'NO CODE
End Sub
Private Sub cmdSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSearch.Click
LoadServer(txtSearch.Text)
End Sub
Private Sub cmdDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDelete.Click
DeleteServer(txtSearch.Tex t)
End Sub
Function LoadServer(ByVal desiredServer As String) As Boolean
Dim doc As New XmlDocument
doc.Load("D:\My Documents\umz.xml")
Dim root As XmlNode = doc.DocumentElement
Dim nodeServer As XmlNode
Dim thisServerName As String
Dim nodeFound As Boolean = False
For Each nodeServer In root.ChildNodes
Try
thisServerName = nodeServer.Attributes("nam e").InnerT ext
If thisServerName.ToLower = desiredServer.ToLower Then
'IF YOU'RE LOADING
txtName.Text = thisServerName
txtPreMVS.Text = nodeServer.SelectSingleNod e("PreMVS" ).InnerTex t
txtPostMVS.Text = nodeServer.SelectSingleNod e("PostMVS ").InnerTe xt
txtPreCS.Text = nodeServer.SelectSingleNod e("PreCS") .InnerText
txtPostCS.Text = nodeServer.SelectSingleNod e("PostCS" ).InnerTex t
txtMisc.Text = nodeServer.SelectSingleNod e("Misc"). InnerText
nodeFound = True
Exit For
End If
Catch ex As Exception
End Try
Next
Return nodeFound
End Function
Here is the XML file:
<?xml version="1.0" standalone="yes"?>
<UMZ>
<Name>ricky</Name>
<PreMVS>some text</PreMVS>
<PostMVS>some text</PostMVS>
<PreCS>some text</PreCS>
<PostCS>some text</PostCS>
<Misc>some text</Misc>
<Name>sue</Name>
<PreMVS>some text</PreMVS>
<PostMVS>some text</PostMVS>
<PreCS>some text</PreCS>
<PostCS>some text</PostCS>
<Misc>some text</Misc>
<Name>bob</Name>
<PreMVS>some text</PreMVS>
<PostMVS>some text</PostMVS>
<PreCS>some text</PreCS>
<PostCS>some text</PostCS>
<Misc>some text</Misc>
</UMZ>
Function DeleteServer(ByVal desiredServer As String) As Boolean
Dim doc As New XmlDocument
doc.Load("D:\My Documents\umz.xml")
Dim root As XmlNode = doc.DocumentElement
Dim nodeServer As XmlNode
Dim thisServerName As String
Dim nodeFound As Boolean = False
For Each nodeServer In root.ChildNodes
Try
thisServerName = nodeServer.Attributes("nam e").InnerT ext
If thisServerName.ToLower = desiredServer.ToLower Then
nodeFound = True
Exit For
End If
Catch ex As Exception
End Try
Next
If nodeFound Then
root.RemoveChild(nodeServe r)
doc.Save("D:\My Documents\umz.xml")
End If
Return nodeFound
End Function
Private Sub cmdAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAdd.Click
'NO CODE
End Sub
Private Sub cmdSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSearch.Click
LoadServer(txtSearch.Text)
End Sub
Private Sub cmdDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDelete.Click
DeleteServer(txtSearch.Tex
End Sub
Function LoadServer(ByVal desiredServer As String) As Boolean
Dim doc As New XmlDocument
doc.Load("D:\My Documents\umz.xml")
Dim root As XmlNode = doc.DocumentElement
Dim nodeServer As XmlNode
Dim thisServerName As String
Dim nodeFound As Boolean = False
For Each nodeServer In root.ChildNodes
Try
thisServerName = nodeServer.Attributes("nam
If thisServerName.ToLower = desiredServer.ToLower Then
'IF YOU'RE LOADING
txtName.Text = thisServerName
txtPreMVS.Text = nodeServer.SelectSingleNod
txtPostMVS.Text = nodeServer.SelectSingleNod
txtPreCS.Text = nodeServer.SelectSingleNod
txtPostCS.Text = nodeServer.SelectSingleNod
txtMisc.Text = nodeServer.SelectSingleNod
nodeFound = True
Exit For
End If
Catch ex As Exception
End Try
Next
Return nodeFound
End Function
Here is the XML file:
<?xml version="1.0" standalone="yes"?>
<UMZ>
<Name>ricky</Name>
<PreMVS>some text</PreMVS>
<PostMVS>some text</PostMVS>
<PreCS>some text</PreCS>
<PostCS>some text</PostCS>
<Misc>some text</Misc>
<Name>sue</Name>
<PreMVS>some text</PreMVS>
<PostMVS>some text</PostMVS>
<PreCS>some text</PreCS>
<PostCS>some text</PostCS>
<Misc>some text</Misc>
<Name>bob</Name>
<PreMVS>some text</PreMVS>
<PostMVS>some text</PostMVS>
<PreCS>some text</PreCS>
<PostCS>some text</PostCS>
<Misc>some text</Misc>
</UMZ>
Function DeleteServer(ByVal desiredServer As String) As Boolean
Dim doc As New XmlDocument
doc.Load("D:\My Documents\umz.xml")
Dim root As XmlNode = doc.DocumentElement
Dim nodeServer As XmlNode
Dim thisServerName As String
Dim nodeFound As Boolean = False
For Each nodeServer In root.ChildNodes
Try
thisServerName = nodeServer.Attributes("nam
If thisServerName.ToLower = desiredServer.ToLower Then
nodeFound = True
Exit For
End If
Catch ex As Exception
End Try
Next
If nodeFound Then
root.RemoveChild(nodeServe
doc.Save("D:\My Documents\umz.xml")
End If
Return nodeFound
End Function
Sorry... i didnt think to post that part up... it's not that much different from your original code:
Sub AddServer()
Dim doc As New XmlDocument
doc.Load("D:\My Documents\UMZ.xml")
Dim root As XmlNode = doc.DocumentElement
Dim EleName As XmlElement = doc.CreateElement("Server" )
Dim ElePreMVS As XmlElement = doc.CreateElement("PreMVS" )
Dim ElePostMVS As XmlElement = doc.CreateElement("PostMVS ")
Dim ElePreCS As XmlElement = doc.CreateElement("PreCS")
Dim ElePostCS As XmlElement = doc.CreateElement("PostCS" )
Dim EleMisc As XmlElement = doc.CreateElement("Misc")
EleName.SetAttribute("name ", txtName.Text)
ElePreMVS.InnerText = txtPreMVS.Text
ElePostMVS.InnerText = txtPostMVS.Text
ElePreCS.InnerText = txtPreCS.Text
ElePostCS.InnerText = txtPostCS.Text
EleMisc.InnerText = txtMisc.Text
EleName.AppendChild(ElePre MVS)
EleName.AppendChild(ElePos tMVS)
EleName.AppendChild(ElePre CS)
EleName.AppendChild(ElePos tCS)
EleName.AppendChild(EleMis c)
root.AppendChild(EleName)
doc.Save("D:\My Documents\UMZ.xml")
End Sub
Sub AddServer()
Dim doc As New XmlDocument
doc.Load("D:\My Documents\UMZ.xml")
Dim root As XmlNode = doc.DocumentElement
Dim EleName As XmlElement = doc.CreateElement("Server"
Dim ElePreMVS As XmlElement = doc.CreateElement("PreMVS"
Dim ElePostMVS As XmlElement = doc.CreateElement("PostMVS
Dim ElePreCS As XmlElement = doc.CreateElement("PreCS")
Dim ElePostCS As XmlElement = doc.CreateElement("PostCS"
Dim EleMisc As XmlElement = doc.CreateElement("Misc")
EleName.SetAttribute("name
ElePreMVS.InnerText = txtPreMVS.Text
ElePostMVS.InnerText = txtPostMVS.Text
ElePreCS.InnerText = txtPreCS.Text
ElePostCS.InnerText = txtPostCS.Text
EleMisc.InnerText = txtMisc.Text
EleName.AppendChild(ElePre
EleName.AppendChild(ElePos
EleName.AppendChild(ElePre
EleName.AppendChild(ElePos
EleName.AppendChild(EleMis
root.AppendChild(EleName)
doc.Save("D:\My Documents\UMZ.xml")
End Sub
ASKER
The add works great. But when I call the function with this command button to search it doesn't do anything. Here is all of my code thus far:
Private Sub cmdAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAdd.Click
AddServer()
End Sub
Private Sub cmdSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSearch.Click
LoadServer(txtSearch.Text)
End Sub
Private Sub cmdDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDelete.Click
DeleteServer(txtSearch.Tex t)
End Sub
Sub AddServer()
Dim doc As New XmlDocument
doc.Load("D:\My Documents\UMZ.xml")
Dim root As XmlNode = doc.DocumentElement
Dim EleName As XmlElement = doc.CreateElement("Server" )
Dim ElePreMVS As XmlElement = doc.CreateElement("PreMVS" )
Dim ElePostMVS As XmlElement = doc.CreateElement("PostMVS ")
Dim ElePreCS As XmlElement = doc.CreateElement("PreCS")
Dim ElePostCS As XmlElement = doc.CreateElement("PostCS" )
Dim EleMisc As XmlElement = doc.CreateElement("Misc")
EleName.SetAttribute("name ", txtName.Text)
ElePreMVS.InnerText = txtPreMVS.Text
ElePostMVS.InnerText = txtPostMVS.Text
ElePreCS.InnerText = txtPreCS.Text
ElePostCS.InnerText = txtPostCS.Text
EleMisc.InnerText = txtMisc.Text
EleName.AppendChild(ElePre MVS)
EleName.AppendChild(ElePos tMVS)
EleName.AppendChild(ElePre CS)
EleName.AppendChild(ElePos tCS)
EleName.AppendChild(EleMis c)
root.AppendChild(EleName)
doc.Save("D:\My Documents\UMZ.xml")
End Sub
Function LoadServer(ByVal desiredServer As String) As Boolean
Dim doc As New XmlDocument
doc.Load("D:\My Documents\umz.xml")
Dim root As XmlNode = doc.DocumentElement
Dim nodeServer As XmlNode
Dim thisServerName As String
Dim nodeFound As Boolean = False
For Each nodeServer In root.ChildNodes
Try
thisServerName = nodeServer.Attributes("Nam e").InnerT ext
If thisServerName.ToLower = desiredServer.ToLower Then
'IF YOU'RE LOADING
txtName.Text = thisServerName
txtPreMVS.Text = nodeServer.SelectSingleNod e("PreMVS" ).InnerTex t
txtPostMVS.Text = nodeServer.SelectSingleNod e("PostMVS ").InnerTe xt
txtPreCS.Text = nodeServer.SelectSingleNod e("PreCS") .InnerText
txtPostCS.Text = nodeServer.SelectSingleNod e("PostCS" ).InnerTex t
txtMisc.Text = nodeServer.SelectSingleNod e("Misc"). InnerText
nodeFound = True
Exit For
End If
Catch ex As Exception
End Try
Next
Return nodeFound
End Function
Function DeleteServer(ByVal desiredServer As String) As Boolean
Dim doc As New XmlDocument
doc.Load("D:\My Documents\umz.xml")
Dim root As XmlNode = doc.DocumentElement
Dim nodeServer As XmlNode
Dim thisServerName As String
Dim nodeFound As Boolean = False
For Each nodeServer In root.ChildNodes
Try
thisServerName = nodeServer.Attributes("Nam e").InnerT ext
If thisServerName.ToLower = desiredServer.ToLower Then
nodeFound = True
Exit For
End If
Catch ex As Exception
End Try
Next
If nodeFound Then
root.RemoveChild(nodeServe r)
doc.Save("D:\My Documents\umz.xml")
End If
Return nodeFound
End Function
Private Sub cmdAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAdd.Click
AddServer()
End Sub
Private Sub cmdSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSearch.Click
LoadServer(txtSearch.Text)
End Sub
Private Sub cmdDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDelete.Click
DeleteServer(txtSearch.Tex
End Sub
Sub AddServer()
Dim doc As New XmlDocument
doc.Load("D:\My Documents\UMZ.xml")
Dim root As XmlNode = doc.DocumentElement
Dim EleName As XmlElement = doc.CreateElement("Server"
Dim ElePreMVS As XmlElement = doc.CreateElement("PreMVS"
Dim ElePostMVS As XmlElement = doc.CreateElement("PostMVS
Dim ElePreCS As XmlElement = doc.CreateElement("PreCS")
Dim ElePostCS As XmlElement = doc.CreateElement("PostCS"
Dim EleMisc As XmlElement = doc.CreateElement("Misc")
EleName.SetAttribute("name
ElePreMVS.InnerText = txtPreMVS.Text
ElePostMVS.InnerText = txtPostMVS.Text
ElePreCS.InnerText = txtPreCS.Text
ElePostCS.InnerText = txtPostCS.Text
EleMisc.InnerText = txtMisc.Text
EleName.AppendChild(ElePre
EleName.AppendChild(ElePos
EleName.AppendChild(ElePre
EleName.AppendChild(ElePos
EleName.AppendChild(EleMis
root.AppendChild(EleName)
doc.Save("D:\My Documents\UMZ.xml")
End Sub
Function LoadServer(ByVal desiredServer As String) As Boolean
Dim doc As New XmlDocument
doc.Load("D:\My Documents\umz.xml")
Dim root As XmlNode = doc.DocumentElement
Dim nodeServer As XmlNode
Dim thisServerName As String
Dim nodeFound As Boolean = False
For Each nodeServer In root.ChildNodes
Try
thisServerName = nodeServer.Attributes("Nam
If thisServerName.ToLower = desiredServer.ToLower Then
'IF YOU'RE LOADING
txtName.Text = thisServerName
txtPreMVS.Text = nodeServer.SelectSingleNod
txtPostMVS.Text = nodeServer.SelectSingleNod
txtPreCS.Text = nodeServer.SelectSingleNod
txtPostCS.Text = nodeServer.SelectSingleNod
txtMisc.Text = nodeServer.SelectSingleNod
nodeFound = True
Exit For
End If
Catch ex As Exception
End Try
Next
Return nodeFound
End Function
Function DeleteServer(ByVal desiredServer As String) As Boolean
Dim doc As New XmlDocument
doc.Load("D:\My Documents\umz.xml")
Dim root As XmlNode = doc.DocumentElement
Dim nodeServer As XmlNode
Dim thisServerName As String
Dim nodeFound As Boolean = False
For Each nodeServer In root.ChildNodes
Try
thisServerName = nodeServer.Attributes("Nam
If thisServerName.ToLower = desiredServer.ToLower Then
nodeFound = True
Exit For
End If
Catch ex As Exception
End Try
Next
If nodeFound Then
root.RemoveChild(nodeServe
doc.Save("D:\My Documents\umz.xml")
End If
Return nodeFound
End Function
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Yes, I can add nodes with the 'AddServer' routine to the XML file. Here is the XML file:
<?xml version="1.0" standalone="yes"?>
<UMZ>
<Server name="ricky">
<PreMVS>some text</PreMVS>
<PostMVS>some text</PostMVS>
<PreCS>some text</PreCS>
<PostCS>some text</PostCS>
<Misc>some text</Misc>
</Server>
<Server name="sue">
<PreMVS>some text</PreMVS>
<PostMVS>some text</PostMVS>
<PreCS>some text</PreCS>
<PostCS>some text</PostCS>
<Misc>some text</Misc>
</Server>
</UMZ>
Am using the below code correctly right??
Private Sub cmdSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSearch.Click
LoadServer(txtSearch.Text)
End Sub
Private Sub cmdDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDelete.Click
DeleteServer(txtSearch.Tex t)
End Sub
Let's say I enter 'sue' in the txtSearch.text box and click cmdSearch button, isn't it supposed to find it and populate the textboxes with the data. Then I would click the cmdDelete button if I wanted 'sue' to be deleted.
<?xml version="1.0" standalone="yes"?>
<UMZ>
<Server name="ricky">
<PreMVS>some text</PreMVS>
<PostMVS>some text</PostMVS>
<PreCS>some text</PreCS>
<PostCS>some text</PostCS>
<Misc>some text</Misc>
</Server>
<Server name="sue">
<PreMVS>some text</PreMVS>
<PostMVS>some text</PostMVS>
<PreCS>some text</PreCS>
<PostCS>some text</PostCS>
<Misc>some text</Misc>
</Server>
</UMZ>
Am using the below code correctly right??
Private Sub cmdSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSearch.Click
LoadServer(txtSearch.Text)
End Sub
Private Sub cmdDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDelete.Click
DeleteServer(txtSearch.Tex
End Sub
Let's say I enter 'sue' in the txtSearch.text box and click cmdSearch button, isn't it supposed to find it and populate the textboxes with the data. Then I would click the cmdDelete button if I wanted 'sue' to be deleted.
ASKER
Sorry, I found the problem...I had the 'n' in name in caps. Thanks for all you help. It works great.
No problem... that was probably my fault, i probably changed it myself and forgot to post up the edited version
e.g.
<?xml version="1.0" standalone="yes"?>
<UMZ>
<Block name="some text">
<PreMVS>some text</PreMVS>
<PostMVS>some text</PostMVS>
<PreCS>some text</PreCS>
<PostCS>some text</PostCS>
<Misc>some text</Misc>
</Block>
<Block name="some more text">
<PreMVS>some more text</PreMVS>
<PostMVS>some more text</PostMVS>
<PreCS>some more text</PreCS>
<PostCS>some more text</PostCS>
<Misc>some more text</Misc>
</Block>
</UMZ>
=================
This would make things easier for yourself as far as i can tell, especially when it comes to modifying the document