Create Edit Remove items in a XML File

jrunowski

Member
Joined
Aug 23, 2006
Messages
15
Programming Experience
Beginner
I was wondering if anyone had some good sites i could visit to read up on the XML namespace. I've been having a lot of trouble writing a vb.net program to read from a xml file edit items in that file and delete items in that file. I know im such rookie, it seems so simple with the XML namespace but from the examples i have been reading i just get lost.

This is what my XML file looks like:
VB.NET:
<?xml version="1.0"?>
<libraries>
<Library Name="LIB01">
  <Server>Server01</Server>
 <Database>DB01</Database>
</Library>
<Library Name="LIB02">
 <Server>Server02</Server>
 <Database>DB02</Database>
</Library>
</libraries>

I want to write a program where i can add to the list of libraries edit the libraries already there or remove a library.

Thanks in advance for the help....this site is amazing everyone is always so helpful.
 
Thanks Raven65, i found an article about XPath while reading through. Here is what i have so far; i know for a fact there is a better way of doing this but it works.

I call the sub with this
VB.NET:
[SIZE=2]edit_xml_file([/SIZE][SIZE=2][COLOR=#800000]"name"[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#800000]"location"[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#800000]"server"[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#800000]"DB"[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#800000]"LIB02"[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#800000]"DEL"[/COLOR][/SIZE][SIZE=2])
[/SIZE]

edit_xml_file Sub:
VB.NET:
[SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] edit_xml_file([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] libname [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] loc [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] server [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] db [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] key [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] mode [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2])
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] filepath [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = [/SIZE][SIZE=2][COLOR=#800000]"C:\libconfig.xml"
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] XDoc [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] XmlDocument()
XDoc.Load(filepath)
[/SIZE][SIZE=2][COLOR=#0000ff]Try
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] XNodes [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] XmlNodeList
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] XNode [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] XmlNode
XNodes = XDoc.DocumentElement.SelectNodes([/SIZE][SIZE=2][COLOR=#800000]"/libraries/Library[@Name='"[/COLOR][/SIZE][SIZE=2] + key + [/SIZE][SIZE=2][COLOR=#800000]"']"[/COLOR][/SIZE][SIZE=2])
[/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] mode = [/SIZE][SIZE=2][COLOR=#800000]"EDIT"[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Each[/COLOR][/SIZE][SIZE=2] XNode [/SIZE][SIZE=2][COLOR=#0000ff]In[/COLOR][/SIZE][SIZE=2] XNodes
XNode.Attributes.ItemOf(0).Value = libname
XNode.ChildNodes(0).InnerText = loc
XNode.ChildNodes(1).InnerText = server
XNode.ChildNodes(2).InnerText = db
[/SIZE][SIZE=2][COLOR=#0000ff]Next
[/COLOR][/SIZE][SIZE=2]XDoc.Save(filepath)
[/SIZE][SIZE=2][COLOR=#0000ff]Else
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Each[/COLOR][/SIZE][SIZE=2] XNode [/SIZE][SIZE=2][COLOR=#0000ff]In[/COLOR][/SIZE][SIZE=2] XNodes
XNode.RemoveAll()
[/SIZE][SIZE=2][COLOR=#0000ff]Next
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2]XDoc.Save(filepath)
[/SIZE][SIZE=2][COLOR=#0000ff]Catch[/COLOR][/SIZE][SIZE=2] ex [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Exception
MsgBox(ex.Message)
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Try
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE]

Basically i can use this sub to edit or delete exisiting nodes based on the value i pass to mode. Everything works for the most part; i know this not the "cleanest" code. The only issue i have now is the remove node code; it leaves the parent tags behind.

xml file after XNode.RemoveAll
VB.NET:
<?xml version="1.0"?>
<libraries>
  <Library Name="LIB01">
    <Location>LOC01</Location>
    <Server>SERVER01</Server>
    <Database>DB01</Database>
  </Library>
  <Library>
  </Library>
</libraries>

Used to be:
VB.NET:
<?xml version="1.0"?>
<libraries>
  <Library Name="LIB01">
    <Location>LOC01</Location>
    <Server>SERVER01</Server>
    <Database>DB01</Database>
  </Library>
  <Library Name="LIB02">
    <Location>LOC02</Location>
    <Server>SERVER02</Server>
    <Database>DB02</Database>
  </Library>
</libraries>
 
Does anyone know why the "Library" xml tags are left after a .removeall on the node i want to delete?

VB.NET:
<?xml version="1.0"?>
<libraries>
[B][COLOR=red] <Library>
  </Library>  [/COLOR][/B]
  <Library Name="LIB02">
    <Location>LOC02</Location>
    <Server>SRV02</Server>
    <Database>DB02</Database>
  </Library>
</libraries>
 
Got it!!!!

i used this instead:
XNode.ParentNode.RemoveChild(XNode)

There was a lot of guess work put into this app, im sure this could be coded better. If anyone has any ideas for better code let me know. Thanks
 
Back
Top