• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 226
  • Last Modified:

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(ElePostMVS)
        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.

0
Richard Kreidl
Asked:
Richard Kreidl
  • 6
  • 5
1 Solution
 
S-TwilleyCommented:
Is there any reason why you haven't grouped the data together into blocks?

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
0
 
Richard KreidlSoftware DeveloperAuthor Commented:
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>


0
 
S-TwilleyCommented:
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("name").InnerText

                If thisServerName.ToLower = desiredServer.ToLower Then
                    'IF YOU'RE LOADING
                    txtName.Text = thisServerName
                    txtPreMVS.Text = nodeServer.SelectSingleNode("PreMVS").InnerText
                    txtPostMVS.Text = nodeServer.SelectSingleNode("PostMVS").InnerText
                    txtPreCS.Text = nodeServer.SelectSingleNode("PreCS").InnerText
                    txtPostCS.Text = nodeServer.SelectSingleNode("PostCS").InnerText
                    txtMisc.Text = nodeServer.SelectSingleNode("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("name").InnerText

                If thisServerName.ToLower = desiredServer.ToLower Then
                    nodeFound = True
                    Exit For
                End If
            Catch ex As Exception

            End Try

        Next

        If nodeFound Then
            root.RemoveChild(nodeServer)
            doc.Save("D:\My Documents\umz.xml")
        End If

        Return nodeFound
    End Function
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
Richard KreidlSoftware DeveloperAuthor Commented:
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??
0
 
Richard KreidlSoftware DeveloperAuthor Commented:
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.Text)
    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("name").InnerText

                If thisServerName.ToLower = desiredServer.ToLower Then
                    'IF YOU'RE LOADING
                    txtName.Text = thisServerName
                    txtPreMVS.Text = nodeServer.SelectSingleNode("PreMVS").InnerText
                    txtPostMVS.Text = nodeServer.SelectSingleNode("PostMVS").InnerText
                    txtPreCS.Text = nodeServer.SelectSingleNode("PreCS").InnerText
                    txtPostCS.Text = nodeServer.SelectSingleNode("PostCS").InnerText
                    txtMisc.Text = nodeServer.SelectSingleNode("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("name").InnerText

                If thisServerName.ToLower = desiredServer.ToLower Then
                    nodeFound = True
                    Exit For
                End If
            Catch ex As Exception

            End Try

        Next

        If nodeFound Then
            root.RemoveChild(nodeServer)
            doc.Save("D:\My Documents\umz.xml")
          End If

        Return nodeFound
    End Function

0
 
S-TwilleyCommented:
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(ElePreMVS)
        EleName.AppendChild(ElePostMVS)
        EleName.AppendChild(ElePreCS)
        EleName.AppendChild(ElePostCS)
        EleName.AppendChild(EleMisc)

        root.AppendChild(EleName)
        doc.Save("D:\My Documents\UMZ.xml")
    End Sub
0
 
Richard KreidlSoftware DeveloperAuthor Commented:
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.Text)
    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(ElePreMVS)
        EleName.AppendChild(ElePostMVS)
        EleName.AppendChild(ElePreCS)
        EleName.AppendChild(ElePostCS)
        EleName.AppendChild(EleMisc)

        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("Name").InnerText

                If thisServerName.ToLower = desiredServer.ToLower Then
                    'IF YOU'RE LOADING
                    txtName.Text = thisServerName
                    txtPreMVS.Text = nodeServer.SelectSingleNode("PreMVS").InnerText
                    txtPostMVS.Text = nodeServer.SelectSingleNode("PostMVS").InnerText
                    txtPreCS.Text = nodeServer.SelectSingleNode("PreCS").InnerText
                    txtPostCS.Text = nodeServer.SelectSingleNode("PostCS").InnerText
                    txtMisc.Text = nodeServer.SelectSingleNode("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("Name").InnerText

                If thisServerName.ToLower = desiredServer.ToLower Then
                    nodeFound = True
                    Exit For
                End If
            Catch ex As Exception

            End Try

        Next

        If nodeFound Then
            root.RemoveChild(nodeServer)
            doc.Save("D:\My Documents\umz.xml")
            End If

        Return nodeFound
    End Function
0
 
S-TwilleyCommented:
have you changed the "D:\My Documents\UMZ.xml" for the location of your document?

and when you say it doesn't do anything... how do you mean? the XML file isn't updated?
0
 
Richard KreidlSoftware DeveloperAuthor Commented:
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.Text)
    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.
0
 
Richard KreidlSoftware DeveloperAuthor Commented:
Sorry, I found the problem...I had the 'n' in name in caps. Thanks for all you help. It works great.
0
 
S-TwilleyCommented:
No problem... that was probably my fault, i probably changed it myself and forgot to post up the edited version
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now