?
Solved

Write TextBox to XML (displayed in DataGrid)

Posted on 2006-04-14
14
Medium Priority
?
618 Views
Last Modified: 2011-10-03
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
0
Comment
Question by:dennis_hs
14 Comments
 
LVL 7

Expert Comment

by:osiris247
ID: 16452666
Check out this tutorial on reading/writing to XML with c#.

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

hth
o
0
 
LVL 2

Expert Comment

by:quoclan
ID: 16452902
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
 
LVL 44

Accepted Solution

by:
Arthur_Wood earned 525 total points
ID: 16453969
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:dennis_hs
ID: 16458363
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
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 16458612
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
 
LVL 1

Author Comment

by:dennis_hs
ID: 16460828
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
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 16461816
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
 
LVL 1

Author Comment

by:dennis_hs
ID: 16464013
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
 
LVL 1

Author Comment

by:dennis_hs
ID: 16464217
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
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 16465874
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
 
LVL 1

Author Comment

by:dennis_hs
ID: 16466155
Well thanks for the effort.. will accept, and try getting things to work right.

/ Dennis
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 16466334
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
 
LVL 1

Author Comment

by:dennis_hs
ID: 16466592
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
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 16466841
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question