XML File Writing

blahblahboring
07-29-2004, 05:48 AM
Ok heres the drift. Ive got an XML File called 'Settings.XML'. I know how to read the XML file and how to write to the XML file from scratch.

However how do i set about only writing to particular nodes for example:

<MyProgram>
<Parent_Node>
<Child>Write Here</Child>
<Child>But Not Here</Child>
</Parent_Node>
</MyProgram>

I want to write in one particular node without having to rewrite the whole file.

please help

MKoslof
07-29-2004, 08:15 AM
For reading and writing via XML and .Net, look here. I just had a discussion about this :)

http://www.visualbasicforum.com/showthread.php?t=179709

blahblahboring
07-31-2004, 06:04 AM
For reading and writing via XML and .Net, look here. I just had a discussion about this :)

http://www.xtremevbtalk.com/showthread.php?t=179709

i understand the concept of what your saying on that post above but i have to say i am struggling to implement your method. is there no way that a node can be edited without touching any other nodes?

i seem to have to restructure and rewrite the whole file if i want to edit one particular node and this is proving to be a bit of a pest.

please help :)

reboot
07-31-2004, 09:20 AM
'assuming xd is a previously opened xml document
Dim Node As Xml.XmlElement = CType(xd.DocumentElement.SelectSingleNode( _
"/configuration/appSettings/add[@key=""" & _
key & """]"), Xml.XmlElement)


Node.Attributes.GetNamedItem("value").Value = valueDon't forget to save the file after you change it.

blahblahboring
07-31-2004, 09:22 AM
excellent thank you

blahblahboring
08-11-2004, 03:46 PM
i looked over the code you submitted and i have to say that i am yet to actually make it work

could you please provide more instructions, thank you

MKoslof
08-12-2004, 07:16 AM
What are you struggling with...I posted a link to another thread and Reboot posted a sample of reading a single node. Where are you having trouble? Can we see your current code?

blahblahboring
08-12-2004, 08:12 AM
What are you struggling with...I posted a link to another thread and Reboot posted a sample of reading a single node. Where are you having trouble? Can we see your current code?

i know how to read nodes no problem i want to write to a single node without interfering with the rest of the document.

please help

is the write code the same as the reading code apart from the obvious differences?

blahblahboring

MKoslof
08-14-2004, 06:35 PM
OK, say you want to append a new node between two other nodes:



Dim xDoc As XmlDocument

xDoc = New XmlDocument
xDoc.Load("C:\tesing\myData.xml")

Dim newEle As XmlNode = xDoc.DocumentElement
Dim post As XmlElement = xDoc.CreateElement("post")

Dim eml As XmlElement = xDoc.CreateElement("email")
eml.InnerText = ("Some text to use")
Dim com As XmlElement = xDoc.CreateElement("comment")
com.InnerText = ("some text to use")

'append all info

newEle.AppendChild(post)
post.AppendChild(eml)
post.AppendChild(com)

'save it back
xDoc.Save("C:\tesing\myData.xml")



Play with this to meet your exact needs

blahblahboring
08-22-2004, 05:45 AM
OK, say you want to append a new node between two other nodes: Play with this to meet your exact needs

the code you have just demonstrated only works for adding nodes, not for editing one particular node.

MKoslof
08-22-2004, 10:02 AM
You said you wanted to write a single node, so that code I posted will do that. What edits do you want to make, do you want to change what data or value is within it? If so, the quickiest method would be to delete the node and then add a new node in its place with the value you want.

blindreaper666
08-26-2004, 10:33 AM
perhaps you could put some kind of identifier value on each node so each one can be read an written to seperatly like

<?xml version="1.0" ?>
<root node>
<childnode id="1">Write here</childnode>
<childnode id="2">Not here</childnode>
</rootnode>

blahblahboring
08-26-2004, 10:35 AM
perhaps you could put some kind of identifier value on each node so each one can be read an written to seperatly like

<?xml version="1.0" ?>
<root node>
<childnode id="1">Write here</childnode>
<childnode id="2">Not here</childnode>
</rootnode>



ill give your method a try blindreaper666 but to be honest i cant wait to get this project finished as i am strongly disliking XML

MKoslof
08-26-2004, 06:25 PM
OK, so did you try to remove the existing node and add a new one with updated information in its place?

And where is this XML coming from? Are you generating your own XML document and then reading it in and changing the values again? What dictates the changes that need to be made. What are you using this XML for and what is the source (are you writing out the XML yourself, creating your own changes based on a certain condition, etc.)

blahblahboring
08-28-2004, 07:58 AM
OK, so did you try to remove the existing node and add a new one with updated information in its place?

And where is this XML coming from? Are you generating your own XML document and then reading it in and changing the values again? What dictates the changes that need to be made. What are you using this XML for and what is the source (are you writing out the XML yourself, creating your own changes based on a certain condition, etc.)

ok please read carefully:

Ive got an XML file called 'Setting.xml.config'.
inside the structure is as follows:

<?xml version="1.0"?>
<My_Program>
<Node_1>
<Child1>text</Child1>
<Child2>text</Child2>
</Node_1>
<Node_2>
<Child1>text</Child1>
<Child2>text</Child2>
<Child3>text</Child3>
<Child4>text</Child4>
</Node_2>
<Node_3>
<Child1>text1</Child1>
<Child2>text2</Child2>
<Child3>text3</Child3>
<Child4>text4</Child4>
<Child5>text5</Child5>
<Child6>text6</Child6>
<Child7>text7</Child7>
<Child8>text8</Child8>
</Node_3>
<Node_4>
<Child1>text1</Child1>
<Child2>text2</Child2>
<Child3>text3</Child3>
</Node_4>
</My_Program>

Right i want to delete all the children in node 3 without having to touch nodes 2 and nodes 4, i dont want to delete node 3 itself just its children or if i cant do that i want to overwrite all the children of node 3.

thanks for your patience

MKoslof
08-28-2004, 09:30 AM
OK, well DELETING Child nodes and values within is not the same as editing the current value of a node..but the concept is basically the same. OK a brief tutorial to get you started:

1) Say I have a simple structure like this:



<EMPLOYEES>
<PERSON>
<NAME>Reboot</NAME>
</PERSON>
<PERSON>
<NAME>MKoslof</NAME>
</PERSON>
</EMPLOYEE>



Now I want to ONLY delete the value for MKoslof within the child node. I could easily delete this node..or recreate it on the fly when done..to delete this value:



Dim XMLdoc as new XmlDocument()

XMLdoc.Load("C:\myXML.xml")

XMLdoc.DocumentElement.RemoveChild(root.DocumentElement.SelectSingleNo de("descendant::NAME[text()='MKoslof']").ParentNode)



OK, now say I have a different XML document and I want to remove the ENTIRE child node..meaning All of Node_1, or Node_2, etc....like your structure




'direct location of Node1 or whatever you want to remove
'another way of referencing the path, via a string object

Dim s As String = "/blah/location to node[@name='Node1']"

Dim XMLdoc As New XmlDocument()
Dim killnode As XmlNode

XMLdoc.Load("C:\test.xml")

'select the node you want, reference the path
killnode = doc.SelectSingleNode(s)
'remove it
killnode.ParentNode.RemoveChild(sysroot)

'save as new file
XMLdoc.Save("c:\temp\Output.xml")

blahblahboring
08-28-2004, 09:33 AM
thanks for your help i realise that my questions are quite vague

MKoslof
08-28-2004, 09:33 AM
Let us know how it turns out.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum