Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How to search and delete elements in a XML file.

Posted on 2005-04-17
11
Medium Priority
?
222 Views
Last Modified: 2013-11-19
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
Comment
Question by:Richard Kreidl
  • 6
  • 5
11 Comments
 
LVL 12

Expert Comment

by:S-Twilley
ID: 13804017
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
 
LVL 2

Author Comment

by:Richard Kreidl
ID: 13805105
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
 
LVL 12

Expert Comment

by:S-Twilley
ID: 13805234
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Author Comment

by:Richard Kreidl
ID: 13805299
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
 
LVL 2

Author Comment

by:Richard Kreidl
ID: 13805368
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
 
LVL 12

Expert Comment

by:S-Twilley
ID: 13805442
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
 
LVL 2

Author Comment

by:Richard Kreidl
ID: 13805494
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
 
LVL 12

Accepted Solution

by:
S-Twilley earned 2000 total points
ID: 13805526
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
 
LVL 2

Author Comment

by:Richard Kreidl
ID: 13805617
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
 
LVL 2

Author Comment

by:Richard Kreidl
ID: 13805667
Sorry, I found the problem...I had the 'n' in name in caps. Thanks for all you help. It works great.
0
 
LVL 12

Expert Comment

by:S-Twilley
ID: 13805682
No problem... that was probably my fault, i probably changed it myself and forgot to post up the edited version
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
Suggested Courses

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question