How to search and delete elements from a XML file?

R

richardkreidl

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.
 
C

Cor Ligthert

Richard,

Why are you not using the dataset. Your XML file is as far as I see
structured in the same way? That will save you a lot of coding.

Cor
 
J

Jay B. Harlow [MVP - Outlook]

Richard,
I would recommend you change your Schema to define each group of elements as
children of a single element, this way each "record" is an element with
child attributes or elements.

Something like:

| <?xml version="1.0" standalone="yes"?>
| <UMZ>
<Record>
| <Name>some text</Name>
| <PreMVS>some text</PreMVS>
| <PostMVS>some text</PostMVS>
| <PreCS>some text</PreCS>
| <PostCS>some text</PostCS>
| <Misc>some text</Misc>
</Record>
<Record>
| <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>
</Record>
| </UMZ>

Then you can use XmlNode.SelectSingleNode to select a "record", and
XmlNode.RemoveChild to remove one of the "records".

Something like:

Dim doc As New XmlDocument
doc.Load("UMZ.xml")

Dim node As XmlNode = doc.SelectSingleNode("/UMZ/Record[Name='some
text']")
node.ParentNode.RemoveChild(node)

doc.Save("UMZ.xml")

"/UMZ/Record[Name='some text']" is an XPath statement that says find the
Record element of the UMZ element in the root, that has a Name element with
the text "some text". If you wanted to find the "some more text" record you
would use: "/UMZ/Record[Name='some more text']"

Note: instead of "Record" for this new containing element name, I would
probably pick something more appropriate to the domain model I am
abstracting...

Hope this helps
Jay

|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.
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top