Write TextBox to XML (displayed in DataGrid)

Hello..

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:

<Projects>
    <Project>
        <ProjectName>ASP.NET</ProjectName>
        <ProjectName>PHP</ProjectName>
        <ProjectName>Cobolt</ProjectName>
    </Project>
</Projects>

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.

/Dennis
LVL 1
dennis_hsAsked:
Who is Participating?
 
Arthur_WoodCommented:
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.WriteStartElement("Projects");
      writer.WriteStartElement("Project");
      writer.WriteElementString("ProjectName", ProjectName);
      writer.WriteEndElement();
      writer.WriteEndElement();
      writer.Flush();
      writer.Close();
}

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

AW
0
 
osiris247Commented:
Check out this tutorial on reading/writing to XML with c#.

http://www.c-sharpcorner.com/Tutorials/ReadWriteXMLTutMellli21.asp

hth
o
0
 
quoclanCommented:
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.
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
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?
0
 
Arthur_WoodCommented:
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:

   setProject(txtWriteToXml.Text);

AW
0
 
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)
{
    setProject(txtWriteToXml.Text);
}

0
 
Arthur_WoodCommented:
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.

AW
0
 
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.
0
 
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.
0
 
Arthur_WoodCommented:
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.

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

/ Dennis
0
 
Arthur_WoodCommented:
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.

AW
0
 
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..

Thanks..

/ Dennis
0
 
Arthur_WoodCommented:
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.

AW
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.