Avatar of John Account
John Account
 asked on

Edit/Enter data into xml file from textbox via code behind

Hello Experts. I don't have a problem creating pages (in vb asp.net 3.5)with a grid to edit data in xml files, and that works sweet. However, I want to create a page in which a wizard guides the user to edit (enter data) one field at a time. So, like if you can tell me how to utilize a textbox and a button, to enter data directly into a certain field, I can figure out the rest in terms of putting it all together via a wizard on one page. (Please see attached  snippet: this is how my xml file is laid out)
<Scripts>
  <Script scriptID="0" Tag="Intro" Value=", blah blah blah" />
  <Script scriptID="1" Tag="Start" Value="blah blah blah...blah blah" />
  <Script scriptID="2" Tag="OpenResponse" Value="blah blah blah blah blah blah" />
</Scripts>

Open in new window

.NET ProgrammingVisual Basic.NETASP.NET

Avatar of undefined
Last Comment
John Account

8/22/2022 - Mon
Bob Learned

That sounds like a complex problem, with a simple description, so where to start, hmmm...
John Account

ASKER
LOL...sorry, TheLearnedOne...I was superfluous in describing my question. All I need help with this this: One textbox that can modify the data of one field in one xml file. And one command button, which runs the action upon its ClickEvent. The field I want to modify is named Value, as in Value="blah blah" illustrated in my attached snippet.

I've gotten to used to modifying my xml data from datagrids and other established controls, and it's been a while since I've actually hard coded this for a textbox.
John Account

ASKER
Of course...any help or guidance offered herein would be greatly appreciate. If not...sigh...oh well, guess I'll just have to rack my brains in countless hours of google research, lol.
Your help has saved me hundreds of hours of internet surfing.
fblack61
Bob Learned

Well, if you are going to do some Google research, then you could start with XmlDocument:

http://weblogs.asp.net/dwahlin/archive/2006/12/14/video-edit-xml-data-with-the-xmldocument-class.aspx
ASKER CERTIFIED SOLUTION
Nikkoli

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Nikkoli

Have you found a solution to your problem, JohnLucio?
John Account

ASKER
Nope, Nikkoli, I haven't. I have tried continually to exercise your idea, but i just can't seem to get anywhere with it. And, it doesn't make sense to me, because it seems as though it should work. I've done other xml references from code behind similiarly, but I can't get concept as to work in this scenario. Guess I'll keep trying.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Nikkoli

How far are you getting, and what errors are you having now? Are you still at square one?

Perhaps we can provide further help after seeing your current code.
John Account

ASKER
Well, one of the many problems I had with this is that I cannot reference the relative path.  Another is  InitializeComponent()
Imports System.Xml
Imports System
Imports System.Data
Imports System.Data.OleDb
Imports System.Collections
Imports System.IO
 
Public Class Default2
    Inherits System.Web.UI.Page
 
    Dim myDoc As XmlDocument
    Dim FileNode As XmlNode
 
    Public Sub New()
        ' This call is required by the Windows Form Designer.
        InitializeComponent()
        myDoc = New XmlDocument
        myDoc.Load("C:\RacTelemarketingComplete\WebSite\App_Data\ScriptsList.xml")
        FileNode = myDoc.SelectSingleNode("/Files/File[@FileID='0']")
        TextBox1.Text = FileNode.Attributes("Value").Value.ToString()
        ' Add any initialization after the InitializeComponent() call.
    End Sub
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If Not (TextBox1.Text = FileNode.Attributes("Value").Value.ToString()) Then
            FileNode.Attributes("Value").Value = TextBox1.Text
            myDoc.Save("C:\RacTelemarketingComplete\WebSite\App_Data\ScriptsList.xml")
        End If
    End Sub
End Class

Open in new window

John Account

ASKER
And here's the xml file using in this testing experiement...
<?xml version="1.0" encoding="utf-8" ?>
<Files>
  <File FileID="0" Value="Selection Required" Desc="Select a market to begin"/>
  <File FileID="1" Value="Website Design" Desc="Web Design Call Scripts" />
  <File FileID="2" Value="Seaway Valley Portals" Desc="Community Websites of the Seaway Valley"/>
</Files>

Open in new window

All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
SOLUTION
Bob Learned

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Nikkoli

Oops, yeah I missed that you were trying to do this in a web page. With TheLearnedOne's suggestions you should find that it goes much further.
John Account

ASKER
Ahhhhhhhh...it's getting there!  One slight problem remaining: Is it possible to use virtual paths as opposed to physical paths?
Nikkoli

I dont believe it can, but you can use a config entry to get the path instead.

in your code behind:
Imports System.Configuration
myDoc.Load(ConfigurationManager.AppSettings("XMLFileKey"))
 
in your web config:
 
<appSettings>
		<add key="XMLFileKey" value="C:\RacTelemarketingComplete\WebSite\App_Data\ScriptsList.xml"/>
	</appSettings>

Open in new window

⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Bob Learned

You can define virtual directories, and use Server.MapPath to get the physical file path, if you wish.
John Account

ASKER
Ahhhhh, Yes---getting somewhere!--thank you both. Presently, the xml value can be read into the textbox. (See attached code). However, when editing the contents of the textbox and clicking the command button, nothing happens except for a postback.
Imports System.Xml
Imports System
Imports System.Data
Imports System.Data.OleDb
Imports System.Collections
Imports System.IO
 
Public Class Default2
    Inherits System.Web.UI.Page
 
    Dim myDoc As XmlDocument
    Dim FileNode As XmlNode
    Dim strCallScripts As String
    Private Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
 
        strCallScripts = "~/App_Data/ScriptsList.xml"
        ' This call is required by the Windows Form Designer.
        'InitializeComponent()
        myDoc = New XmlDocument
        'myDoc.Load("C:\RacTelemarketingComplete\WebSite\App_Data\ScriptsList.xml")
        myDoc.Load(Server.MapPath(strCallScripts))
        FileNode = myDoc.SelectSingleNode("/Files/File[@FileID='2']")
        TextBox1.Text = FileNode.Attributes("Value").Value.ToString()
        ' Add any initialization after the InitializeComponent() call.
 
    End Sub
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If Not (TextBox1.Text = FileNode.Attributes("Value").Value.ToString()) Then
            FileNode.Attributes("Value").Value = TextBox1.Text
            myDoc.Save(Server.MapPath(strCallScripts))
        End If
    End Sub
 
End Class

Open in new window

Nikkoli

Played around with this some, and I see what you are saying.

To fix this, only populate the textbox when it is not a postback as such:
If not Page.IsPostBack Then
  TextBox1.Text = FileNode.Attributes("Value").Value.ToString()
End If
 
I also put the textbox as autopostback="true" when playing.

Open in new window

This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
John Account

ASKER
Doesn't work, Nikkoli. That's only giving the illusion that the data has been updated.
Bob Learned

Can you debug this code?

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If Not (TextBox1.Text = FileNode.Attributes("Value").Value.ToString()) Then
            FileNode.Attributes("Value").Value = TextBox1.Text
            myDoc.Save(Server.MapPath(strCallScripts))
        End If
    End Sub

Is this condition satisfied?

    If Not (TextBox1.Text = FileNode.Attributes("Value").Value.ToString()) Then
Nikkoli

In mine it was actually updating John as could be seen by exiting and then re-starting the website and seeing the new value.  I would type a new value into the textbox, then when I left the textbox it would repost.  I would still have to click the button to save the data though.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
John Account

ASKER
Nikkoli, please come here http://www.versacore.info/telemarketing/Default2.aspx and change the value. Now, close the browser. Open the browser and return there. You will notice that it has indeed been changed. But, change it again, then close the browser, and re-open the browser and return. This time you will notice that in fact it had not been changed, although it had seemed to have been changed. Attached is code behind as on the server.
Imports System.Xml
Imports System
Imports System.Data
Imports System.Data.OleDb
Imports System.Collections
Imports System.IO
 
Public Class Default2
    Inherits System.Web.UI.Page
 
    Dim myDoc As XmlDocument
    Dim FileNode As XmlNode
    Dim strCallScripts As String
    Private Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
        strCallScripts = "~/App_Data/TestList.xml"
        myDoc = New XmlDocument
 
        myDoc.Load(Server.MapPath(strCallScripts))
        FileNode = myDoc.SelectSingleNode("/Files/File[@FileID='2']")
        If Not Page.IsPostBack Then
            TextBox1.Text = FileNode.Attributes("Value").Value.ToString()
        End If
    End Sub
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        FileNode.Attributes("Value").Value = TextBox1.Text
        myDoc.Save(Server.MapPath(strCallScripts))
    End Sub
End Class

Open in new window

Nikkoli

I went back four times and followed the following procedure:
Navigate to site
Change text, hit enter
Click button
close all open browsers
navigate to site
change text, hit enter
click button
close browser
etc...
 
every time I saw the latest enter from before closing the browser.
John Account

ASKER
Hmmmnnnnnn...wow, you're right, Nikkoli...I think! Perhaps I'm losing my mind!!--anyways, I guess that would be a question for another kind of expert. A mental expert, lol.

Thank you so much, Nikkoli, and you TheLearnedOne, for helping me to see this thing through! Much appreciated!!
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
John Account

ASKER
Thank you very much