Write TextBox to XML (displayed in DataGrid)


I have to submit the content of a TextBox to an XML file. The XML is then displayed in a DataGrid (or GridView, doesn't matter).

The XML looks like this:


I have TextBox = txtWriteToXml, DataGrid = dgXml and Button = btnSubmit.

What's the best way?

It's ASP.NET w/ C# by the way

Thanks in advance.

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Check out this tutorial on reading/writing to XML with c#.


About reading/writing to XML, you can see comment of orisis247 ...

I have some suggestion about your way (TextBox -> Xml -> Grid) :
- if you use TextBox -> XML -> Grid, updating XML will happen more, and if you save XML in your server --> high network traffic
So, i propose one way : from TextBox, when you click button, your data is inputted into Grid (via DataSet), when you input enough data, click another button to transfer data in Grid to server in XML format ... With this way, you can low network traffic, and this is simple to do because DataSet support output data in XML (WriteXML method)
But it has a risk : when user put much data into grid, the browser is closed by accident --> user has time-cosuming to input again ... To solve this, you can use javascript to save data in cookie temporary regularly and so more ...

Hope you like this.
here is some C# code that will accomplish what you are asking:

public static void setProject( string ProjectName)
      System.IO.StreamWriter w = new System.IO.StreamWriter(<path to the XML file on disk>);
      XmlTextWriter writer = new XmlTextWriter(w);
      writer.Formatting = Formatting.Indented;
      writer.WriteElementString("ProjectName", ProjectName);

then you pass the textBox.text as the argument to this function.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

dennis_hsAuthor Commented:
Thanks AW. But given my current "level" of C# knowledge I might need a little guidense.

How do I use your code with my textbox and button?
is your btnSubmit a SUBMIT type button?

you can place the code I gav4e you, for the procedure setProject in the code-behind of you asp.NET page, and then call this procedure from the event handler of your button, like this:


dennis_hsAuthor Commented:
Hmm..  I can't get to work right. There's no errors, but it doesn't write what I type into the xml file. It's like nothing happens when I click the button.

Just to be sure, it is like this you mean right?

protected void btnSubmit_Click(object sender, EventArgs e)

did you add the code for the procedure to the same code-behind file?

did you set the correct path to the fioe that you want to write to?

I have a C# ASP.NET application that does essentially what you have, and it works perfectly for me.  That is how I had the code right at hand.

dennis_hsAuthor Commented:
I get an error about rights i don't know how to get around:

Exception Details: System.UnauthorizedAccessException: Access to the path 'c:\Inetpub\wwwroot\xml\data.xml' is denied.

About the files, I've made a new project only with Default.aspx, Default.aspx.cs, data.xml and Web.Config just to be sure.
dennis_hsAuthor Commented:
I tried to share the folder with full rights, but now it only writes one post. If I submit fx. ASP.NET and then PHP right after, PHP is the only one standing there. So at any given time there is only one entrance in the XML file.
sorry, but I can't help you with this one.  Talk to your system admin - maybe they can help you on thie problem.  But this is the reason why you can save the text.

dennis_hsAuthor Commented:
Well thanks for the effort.. will accept, and try getting things to work right.

/ Dennis
so are you trying to ADD posts the the file, that you already have?  The code theat I gave you will only write one at a time.  If you need to add, thenyou need to read in the existing file, and then write out what was ther before, then add the new line, before writing the two EndElements:

      //loop through the reader, to write the project names that were already there, then
      writer.WriteElementString("ProjectName", ProjectName);
      writer.WriteEndElement();   // to close the Project Element
      writer.WriteEndElement();   // to close the Projects Element

You did not make you complete requiremetns clear at the outset.

dennis_hsAuthor Commented:
Sorry if I didn't make myself clear. Yes, I'm supposed to add one entrance after another..

I've tried to figure out the loops, but kinda failed to do so.. Any thoughts would be appreciated. Just some simple stuff perhaps..


/ Dennis
as you add items to the XML file, the necxt time read the file, get the items that have already been added, then write those items BACK to the file, adding the new item after the previous ones have been re-added, then close the two Elements.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.