Editing an XML file problem

the edit is not being saved

Page 1 of 1

3 Replies - 11403 Views - Last Post: 05 February 2007 - 11:14 PM Rate Topic: -----

#1 ross_petersen  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 65
  • Joined: 06-August 05

Editing an XML file problem

Posted 04 February 2007 - 01:37 PM

:) Hi folks

I have a separate XML file for a VB.NET project (framework version 1.1) that is being used to persist info between sessions.

the XML file is called DefaultFolders.xml and looks like this:

<?xml version="1.0"?>
<DEFAULTFOLDERS>
<DEFAULTSEARCHFOLDER>C:\Documents and Settings\Ross\My Documents\Programming\Projects\Ultra106Five\SoundFiles\SampleSoundFiles</DEFAULTSEARCHFOLDER>
<DEFAULTDESTFOLDER></DEFAULTDESTFOLDER>
</DEFAULTFOLDERS>

Now I have a Sub Proc that is as follows:

Private Sub SetDefaultSearchFolder(ByVal strFolder As String)

		'this sub proc writes the path of the default search folder to the xml file
		'DefaultSearchFolder.xml 
		'the file is in the same directory as the application

		Dim xmlDoc As New XmlDocument
		Dim tempNode As XmlNode

		xmlDoc.Load("C:\Documents and Settings\Ross\My Documents\Programming\Projects\Ultra106Five\SoundFiles\SearchForSoundFiles\DefaultFolders.xml")

		'navigate to the element entitled <DEFAULTSEARCHFOLDER>
		For Each tempNode In xmlDoc.DocumentElement

			If tempNode.Name = "DEFAULTSEARCHFOLDER" Then

				tempNode.InnerText = strFolder
				xmlDoc.Save(xmlDoc.Name.ToString)


			End If
		Next

	End Sub



The proc is called from another proc and appears to run without error.

When I step through the code, the line that says:
tempNode.InnerText = strFolder



appears to work OK, but the xmlDoc.Save doesn't appear to do anything as when I examine the file afterwards it hasn't changed.

So, if somebody could show me the error of my ways (LOL), I would muchly appreciate it.

Kind regards

Ross Petersen

Is This A Good Question/Topic? 0
  • +

Replies To: Editing an XML file problem

#2 Noldona  Icon User is offline

  • Addicted to DIC

Reputation: 5
  • View blog
  • Posts: 1,051
  • Joined: 02-July 02

Re: Editing an XML file problem

Posted 05 February 2007 - 11:03 AM

It looks right to me. Although it has been a while since I dealt with VB. The only thing I could figure that might be causing the problem is the save command. Check the value of the member variable you are passing it and see if it contains the file you loaded with path. It may contain the file name but might be putting it in a different path then where it is read from. In my project, I have just had the xml files in the same directory as the program and just loaded the xml file by name without a path and it has been working fine for me.
Was This Post Helpful? 0
  • +
  • -

#3 ross_petersen  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 65
  • Joined: 06-August 05

Re: Editing an XML file problem

Post icon  Posted 05 February 2007 - 01:56 PM

View PostNoldona, on 5 Feb, 2007 - 11:03 AM, said:

It looks right to me. Although it has been a while since I dealt with VB. The only thing I could figure that might be causing the problem is the save command. Check the value of the member variable you are passing it and see if it contains the file you loaded with path. It may contain the file name but might be putting it in a different path then where it is read from. In my project, I have just had the xml files in the same directory as the program and just loaded the xml file by name without a path and it has been working fine for me.


Hi Noldona

Thanks for that, I checked out your suggestions.

First of all, I did a search of the PC to see if there were any other copies of the same file around - but no joy.

Secondly, I stepped through the code and for the line :

xmlDoc.Save(xmlDoc.Name.ToString)



the xmlDoc.Name.ToString is showing the value "#document" in the immediate window.

Any ideas on what "#document" means?

thanks for your help.

Regards

Ross :)
Was This Post Helpful? 0
  • +
  • -

#4 ross_petersen  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 65
  • Joined: 06-August 05

Re: Editing an XML file problem

Post icon  Posted 05 February 2007 - 11:14 PM

Hi folks - SUCCESS

Here is the corrected code:

Private Sub SetDefaultSearchFolder(ByVal strFolder As String)

		'this sub proc writes the path of the default search folder to the xml file
		'DefaultSearchFolder.xml 
		'the file is in the same directory as the application

		Dim xmlDoc As New XmlDocument
		Dim tempNode As XmlNode


		xmlDoc.Load("C:\Documents and Settings\Ross\My Documents\Programming\Projects\Ultra106Five\SoundFiles\SearchForSoundFiles\DefaultFolders.xml")

		'navigate to the element entitled <DEFAULTSEARCHFOLDER>
		For Each tempNode In xmlDoc.DocumentElement

			If tempNode.Name = "DEFAULTSEARCHFOLDER" Then

				tempNode.InnerText = strFolder
				'xmlDoc.Save(xmlDoc.Name.ToString)

			End If

		Next

		Dim writer As New XmlTextWriter("C:\Documents and Settings\Ross\My Documents\Programming\Projects\Ultra106Five\SoundFiles\SearchForSoundFiles\DefaultFolders.xml", System.Text.ASCIIEncoding.ASCII)


		writer.Formatting = Formatting.Indented
		xmlDoc.WriteContentTo(writer)
		writer.Close()

	End Sub



Many thanks

Ross :) :) :) :)




View Postross_petersen, on 5 Feb, 2007 - 01:56 PM, said:

View PostNoldona, on 5 Feb, 2007 - 11:03 AM, said:

It looks right to me. Although it has been a while since I dealt with VB. The only thing I could figure that might be causing the problem is the save command. Check the value of the member variable you are passing it and see if it contains the file you loaded with path. It may contain the file name but might be putting it in a different path then where it is read from. In my project, I have just had the xml files in the same directory as the program and just loaded the xml file by name without a path and it has been working fine for me.


Hi Noldona

Thanks for that, I checked out your suggestions.

First of all, I did a search of the PC to see if there were any other copies of the same file around - but no joy.

Secondly, I stepped through the code and for the line :

xmlDoc.Save(xmlDoc.Name.ToString)



the xmlDoc.Name.ToString is showing the value "#document" in the immediate window.

Any ideas on what "#document" means?

thanks for your help.

Regards

Ross :)

Was This Post Helpful? 1

Page 1 of 1