Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to search and delete elements from a XML file?

Status
Not open for further replies.

rkckjk

IS-IT--Management
Apr 27, 2001
34
US
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.

 
The joy of ado.Net

dim dsFromXML as New Dataset
dim dtMyTable as datatable
dsFromXML.ReadXML(filename)
dtMyTable = dsFromXML.Table(0)

Use dtMyTable.Select to find specific rows (returns an array or datarows). You can use binding to link the text boxes to the data table, and dtMyTable.delete to remove entries. When you are done, use dsFromXML.WriteXML to save the changes back to the XML file.

-Rick


----------------------
[banghead]If you're about to post an ASP.Net question,
please don't do it in the VB.Net forum[banghead]

[monkey] I believe in killer coding ninja monkeys.[monkey]
 
I agree with Rick. Datasets are far more easy to do what you want.

Christiaan Baes
Belgium

If you want to get an answer read this FAQ faq796-2540
There's no such thing as a winnable war - Sting
 
I'm not familiar with using Dataset tables. Could you add elements with this method also??
 
yup. The dataset reads the XML into a data table. Once you have a data table you can add rows, delete rows, select rows, create views, bind controls, and do all sorts of stuff. There are tons of examples of using Datatables here, in the FAQ, and on google.

-Rick

----------------------
[banghead]If you're about to post an ASP.Net question,
please don't do it in the VB.Net forum[banghead]

[monkey] I believe in killer coding ninja monkeys.[monkey]
 
Are datasets and data tables controls that I put on the form or are you talking about using tables in Access with VB .Net? Could you write some code to get me in the right direction??
 
goto google and type in ado.net sample xml I'm sure you'll come up with something. The DataSet, Datatable, and data row are not GUI elements.

-Rick

----------------------
[banghead]If you're about to post an ASP.Net question,
please don't do it in the VB.Net forum[banghead]

[monkey] I believe in killer coding ninja monkeys.[monkey]
 
Do you guys have any recommendations on making the XML multi-user accessible, for small DB applications that can store the data in XML?

 
Nothing easy. Err, nothing safe and easy. The fastest route would probrably be to use a locking table that would track which users are looking at which record and would prevent other users from modifying it.

-Rick

----------------------
[banghead]If you're about to post an ASP.Net question,
please don't do it in the VB.Net forum[banghead]

[monkey] I believe in killer coding ninja monkeys.[monkey]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top