Go Back  Xtreme Visual Basic Talk > Visual Basic .NET (2002/2003/2005/2008, including Express editions) > .NET File I/O and Registry > XML File Writing


Reply
 
Thread Tools Display Modes
  #1  
Old 07-29-2004, 05:48 AM
blahblahboring blahblahboring is offline
Regular
 
Join Date: Jul 2003
Location: Warrington
Posts: 61
Default XML File Writing


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:

Code:
<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
Reply With Quote
  #2  
Old 07-29-2004, 08:15 AM
MKoslof's Avatar
MKoslof MKoslof is offline
Cum Grano Salis

Retired Moderator
* Guru *
 
Join Date: Jul 2002
Location: Baltimore, Maryland
Posts: 14,636
Default

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
__________________
"Artificial Intelligence is no match for natural stupidity." ~unknown
Reply With Quote
  #3  
Old 07-31-2004, 06:04 AM
blahblahboring blahblahboring is offline
Regular
 
Join Date: Jul 2003
Location: Warrington
Posts: 61
Default

Quote:
Originally Posted by MKoslof
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
Reply With Quote
  #4  
Old 07-31-2004, 09:20 AM
reboot's Avatar
reboot reboot is offline
Keeper of foo

Retired Moderator
* Guru *
 
Join Date: Nov 2001
Location: Graceland
Posts: 15,612
Default

Code:
'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 = value
Don't forget to save the file after you change it.
__________________
~ Quod non mortiferum, fortiorem me facit ~

Avatar by lebb
Reply With Quote
  #5  
Old 07-31-2004, 09:22 AM
blahblahboring blahblahboring is offline
Regular
 
Join Date: Jul 2003
Location: Warrington
Posts: 61
Default

excellent thank you
Reply With Quote
  #6  
Old 08-11-2004, 03:46 PM
blahblahboring blahblahboring is offline
Regular
 
Join Date: Jul 2003
Location: Warrington
Posts: 61
Default

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
__________________
Come To A BrighterWorld.co.uk
Reply With Quote
  #7  
Old 08-12-2004, 07:16 AM
MKoslof's Avatar
MKoslof MKoslof is offline
Cum Grano Salis

Retired Moderator
* Guru *
 
Join Date: Jul 2002
Location: Baltimore, Maryland
Posts: 14,636
Default

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?
__________________
"Artificial Intelligence is no match for natural stupidity." ~unknown
Reply With Quote
  #8  
Old 08-12-2004, 08:12 AM
blahblahboring blahblahboring is offline
Regular
 
Join Date: Jul 2003
Location: Warrington
Posts: 61
Default

Quote:
Originally Posted by MKoslof
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
__________________
Come To A BrighterWorld.co.uk
Reply With Quote
  #9  
Old 08-14-2004, 06:35 PM
MKoslof's Avatar
MKoslof MKoslof is offline
Cum Grano Salis

Retired Moderator
* Guru *
 
Join Date: Jul 2002
Location: Baltimore, Maryland
Posts: 14,636
Default

OK, say you want to append a new node between two other nodes:

Code:
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
__________________
"Artificial Intelligence is no match for natural stupidity." ~unknown
Reply With Quote
  #10  
Old 08-22-2004, 05:45 AM
blahblahboring blahblahboring is offline
Regular
 
Join Date: Jul 2003
Location: Warrington
Posts: 61
Default

Quote:
Originally Posted by MKoslof
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.
__________________
Come To A BrighterWorld.co.uk
Reply With Quote
  #11  
Old 08-22-2004, 10:02 AM
MKoslof's Avatar
MKoslof MKoslof is offline
Cum Grano Salis

Retired Moderator
* Guru *
 
Join Date: Jul 2002
Location: Baltimore, Maryland
Posts: 14,636
Default

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.
__________________
"Artificial Intelligence is no match for natural stupidity." ~unknown
Reply With Quote
  #12  
Old 08-26-2004, 10:33 AM
blindreaper666's Avatar
blindreaper666 blindreaper666 is offline
Junior Contributor
 
Join Date: Aug 2004
Posts: 317
Default

perhaps you could put some kind of identifier value on each node so each one can be read an written to seperatly like
Code:
<?xml version="1.0" ?> <root node> <childnode id="1">Write here</childnode> <childnode id="2">Not here</childnode> </rootnode>
Reply With Quote
  #13  
Old 08-26-2004, 10:35 AM
blahblahboring blahblahboring is offline
Regular
 
Join Date: Jul 2003
Location: Warrington
Posts: 61
Default

Quote:
Originally Posted by blindreaper666
perhaps you could put some kind of identifier value on each node so each one can be read an written to seperatly like
Code:
<?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
__________________
Come To A BrighterWorld.co.uk
Reply With Quote
  #14  
Old 08-26-2004, 06:25 PM
MKoslof's Avatar
MKoslof MKoslof is offline
Cum Grano Salis

Retired Moderator
* Guru *
 
Join Date: Jul 2002
Location: Baltimore, Maryland
Posts: 14,636
Default

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.)
__________________
"Artificial Intelligence is no match for natural stupidity." ~unknown
Reply With Quote
  #15  
Old 08-28-2004, 07:58 AM
blahblahboring blahblahboring is offline
Regular
 
Join Date: Jul 2003
Location: Warrington
Posts: 61
Default

Quote:
Originally Posted by MKoslof
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:

Code:
<?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
__________________
Come To A BrighterWorld.co.uk
Reply With Quote
  #16  
Old 08-28-2004, 09:30 AM
MKoslof's Avatar
MKoslof MKoslof is offline
Cum Grano Salis

Retired Moderator
* Guru *
 
Join Date: Jul 2002
Location: Baltimore, Maryland
Posts: 14,636
Default

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:

Code:
<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:

Code:
Dim XMLdoc as new XmlDocument() XMLdoc.Load("C:\myXML.xml") XMLdoc.DocumentElement.RemoveChild(root.DocumentElement.SelectSingleNode("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

Code:
'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")
__________________
"Artificial Intelligence is no match for natural stupidity." ~unknown
Reply With Quote
  #17  
Old 08-28-2004, 09:33 AM
blahblahboring blahblahboring is offline
Regular
 
Join Date: Jul 2003
Location: Warrington
Posts: 61
Default

thanks for your help i realise that my questions are quite vague
__________________
Come To A BrighterWorld.co.uk
Reply With Quote
  #18  
Old 08-28-2004, 09:33 AM
MKoslof's Avatar
MKoslof MKoslof is offline
Cum Grano Salis

Retired Moderator
* Guru *
 
Join Date: Jul 2002
Location: Baltimore, Maryland
Posts: 14,636
Default

Let us know how it turns out.
__________________
"Artificial Intelligence is no match for natural stupidity." ~unknown
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->