473,564 Members | 2,759 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_C lick(ByVal sender As System.Object, ByVal e
As System.EventArg s) Handles cmdAddElement.C lick

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

Dim root As XmlNode = doc.DocumentEle ment
Dim EleName As XmlElement = doc.CreateEleme nt("Name")
Dim ElePreMVS As XmlElement = doc.CreateEleme nt("PreMVS")
Dim ElePostMVS As XmlElement = doc.CreateEleme nt("PostMVS")
Dim ElePreCS As XmlElement = doc.CreateEleme nt("PreCS")
Dim ElePostCS As XmlElement = doc.CreateEleme nt("PostCS")
Dim EleMisc As XmlElement = doc.CreateEleme nt("Misc")

EleName.InnerTe xt = txtName.Text
ElePreMVS.Inner Text = txtPreMVS.Text
ElePostMVS.Inne rText = txtPostMVS.Text
ElePreCS.InnerT ext = txtPreCS.Text
ElePostCS.Inner Text = txtPostCS.Text
EleMisc.InnerTe xt = txtMisc.Text

root.AppendChil d(EleName)
root.AppendChil d(ElePreMVS)
root.AppendChil d(ElePostMVS)
root.AppendChil d(ElePreCS)
root.AppendChil d(ElePostCS)
root.AppendChil d(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 4591
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.SelectS ingleNode to select a "record", and
XmlNode.RemoveC hild to remove one of the "records".

Something like:

Dim doc As New XmlDocument
doc.Load("UMZ.x ml")

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

doc.Save("UMZ.x ml")

"/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***********@ northwesternmut ual.com> wrote in message
news:11******** *************@f 14g2000cwb.goog legroups.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_C lick(ByVal sender As System.Object, ByVal e
| As System.EventArg s) Handles cmdAddElement.C lick
|
| Dim doc As New XmlDocument
| doc.Load("c:UMZ .xml")
|
| Dim root As XmlNode = doc.DocumentEle ment
| Dim EleName As XmlElement = doc.CreateEleme nt("Name")
| Dim ElePreMVS As XmlElement = doc.CreateEleme nt("PreMVS")
| Dim ElePostMVS As XmlElement = doc.CreateEleme nt("PostMVS")
| Dim ElePreCS As XmlElement = doc.CreateEleme nt("PreCS")
| Dim ElePostCS As XmlElement = doc.CreateEleme nt("PostCS")
| Dim EleMisc As XmlElement = doc.CreateEleme nt("Misc")
|
| EleName.InnerTe xt = txtName.Text
| ElePreMVS.Inner Text = txtPreMVS.Text
| ElePostMVS.Inne rText = txtPostMVS.Text
| ElePreCS.InnerT ext = txtPreCS.Text
| ElePostCS.Inner Text = txtPostCS.Text
| EleMisc.InnerTe xt = txtMisc.Text
|
| root.AppendChil d(EleName)
| root.AppendChil d(ElePreMVS)
| root.AppendChil d(ElePostMVS)
| root.AppendChil d(ElePreCS)
| root.AppendChil d(ElePostCS)
| root.AppendChil d(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
15635
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. *FILE29*.TXT). I'm looking for a way to do this in either Visual Basic or C(++). Thanks!
2
11504
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 understand how to add the new names to the list. I have 3 questions: 1) How can I search through the table rows to find the rows to be changed...
36
72032
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 can type straight away Thanks in advance
5
2234
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 name="COURSE_CODE">ADA1W0</item> <item name="SECTION_NUMBER">33</item> <item name="COURSE_DESC_CODE"/> <item name="COURSE_TYPE">DS</item>
6
6412
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. Here is a description of the three functions: Insert: Accepts as input the array and the element you want to insert into the array. The function...
18
10064
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 them. the reason is because in the following code, i show/hide a file field by checkbox toggle... however, if a file field selects a file before it's...
12
1790
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, of which the contents are submitted to a search page. I know I could just send the form element data to a search page, but in this app, the user is...
9
1705
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. I have written my own catalog program and it has xml files to cache info from tags etc. To ensure that the process goes ok I went to delete all...
0
2712
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, cannot serch by combine 3 table) Example I have the query table below, how do I make the code to seach based on the query from this: SELECT...
0
7583
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8106
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7642
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7950
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6255
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5213
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3643
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1200
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
924
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.