473,508 Members | 2,363 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to search and delete elements from 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.

Nov 21 '05 #1
2 4581
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
Nov 21 '05 #2
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

<ri***********@northwesternmutual.com> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...
|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.
|
Nov 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
15623
by: Ben | last post by:
Greetings, I am looking for a way to search for and delete files based on a pattern mask. For example, the search method would find all files matching a certain pattern containing wildcards (e.g....
2
11491
by: Mike | last post by:
My page populates a table with a list of names and other information from a JavaScript object. I receive changes (adds, change & delete) to that list, convert it into a JavaScript object. I do...
36
72021
by: spence | last post by:
Hi All How do I make it so that when a user clicks in a search text field, the default entry (in this case "Search") is removed automatically - they are then faced with a blank search box and...
5
2226
by: DarthDaddy | last post by:
I hope to explain this properly. Here is a sample section of a file I am working with: <achievements> <achievement> <item name="COURSE_COMPLETION_DATE">19930630</item> <item...
6
6406
by: flash | last post by:
write a program that manipulates arrays of integers. The main program should call three functions: Insert, Delete, and Search. The Insert function should call a function Sort that sorts the array. ...
18
10055
chunk1978
by: chunk1978 | last post by:
hi everyone. i just started learning how to dynamically show/hide elements with a checkbox toggle. i would like to know if it's possible to delete/rewrite elements instead of simply show/hide...
12
1778
by: David | last post by:
Hi, I am trying to achieve the following: 1/ To have a standard form in an asp web page which has various check boxes and radio buttons. As a user selects a form item it updates a text box,...
9
1703
by: Lloyd Sheen | last post by:
For all those who don't think that a recursive search of files in folders is a good thing in the Microsoft.VisualBasic.FileIO.FileSystem namespace listen to this. I am reorg my mp3 collection. ...
0
2707
by: JamesOo | last post by:
I have the code below, but I need to make it searchable in query table, below code only allowed seach the table which in show mdb only. (i.e. have 3 table, but only can search either one only,...
0
7377
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7036
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7489
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5624
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5047
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3191
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
762
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
414
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.