hi,
how can i load a xml file, then be able to add, delete, rename nodes from a windows form? it would be nice if someone could send me some sample code, thanks
Printable View
hi,
how can i load a xml file, then be able to add, delete, rename nodes from a windows form? it would be nice if someone could send me some sample code, thanks
Here we go:
dim xmlDoc As New XmlDocument()
'*load xml file
xmlDoc.Load("c:\some.xml")
'*find node
dim node1 as xmlelement
node1=rootNode.SelectSingleNode("child::somenode")
dim str as string
'*get text from node
str=node1.innertext
'* set some text to node and save xml file
node1.innertext="some text"
xmlDoc.save("c:\some.xml")
I hope that helps.
regard j
thanks,
and how do we add and remove a complete node?
Adding and removing work a lot like in a normal collection. You can select a node and then use Remove to remove it. To add then select the parent of the node you want to add then create and append the new node. Check out the XMLDocument object methods or documentation and you should be able to figure it out.
what if we have something like :
<parent>
<child id=1/>
<child id=3/>
<child id=4/>
</parent>
and wanted to add a child node , between 1 and 3 ( <child id=2/> ), is it possible?
( the order is important in my case )
thanks again
and can we find out how deep are xml file is ??? ( the depth of the deepest node )
It can be hard (or if not I don't knwo how) to control the physical order of the nodes but IDs can be ordered easy enough. I believe there is an Insert or InsertAt method that you can use to try and control the physical order. Primarily what is reserved is the hierarchy structure, child to parent and so on. Really the key to getting tricky with it is learning XPath queries.
http://www.zvon.org/xxl/XPathTutoria.../example1.html
http://www.w3.org/TR/xpath
If you have some sample xml data you can post or upload then I can probably help a little more.
The DOM Object is a very powerful xml tool. Everything you are describing pertains to retrieving the current node. Once you have a pointer to the node, you add, remove, insert before, after, etc.. I have extensive experience with xml / xslt / dom, so feel free to ask and questions and/or need examples.