Link to home
Start Free TrialLog in
Avatar of dennis_hs
dennis_hsFlag for Denmark

asked on

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
Avatar of osiris247
osiris247
Flag of United States of America image

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

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

hth
o
Avatar of quoclan
quoclan

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.
ASKER CERTIFIED SOLUTION
Avatar of Arthur_Wood
Arthur_Wood
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dennis_hs

ASKER

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:

   setProject(txtWriteToXml.Text);

AW
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);
}

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

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

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