| | |
Append in xml document
Please support our C# advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jun 2006
Posts: 3
Reputation:
Solved Threads: 0
I have an application that saves some data in xml document.
The user can add a new data to xml document.
The problem is the old data will be overwritten by the new one,How could I solve that??
Here is my code for that method which still needs "APPENDING":
The user can add a new data to xml document.
The problem is the old data will be overwritten by the new one,How could I solve that??
Here is my code for that method which still needs "APPENDING":
XmlTextWriter textWritter=new XmlTextWriter("F:/Documents and Settings/Administrator/Desktop/Account.xml", null); textWritter.WriteStartDocument(); textWritter.WriteStartElement("USER"); //User nametextWritter.WriteStartElement("UserName",""); textWritter.WriteString(txtUsrName.Text.Trim()); textWritter.WriteEndElement(); //Email textWritter.WriteStartElement("Email",""); textWritter.WriteString(txtEmail.Text.Trim()); textWritter.WriteEndElement(); textWritter.WriteEndDocument(); textWritter.Close();
try this.
it creates its own file stream and sets the access to append.
it replaces your fiest line
it creates its own file stream and sets the access to append.
it replaces your fiest line
C# Syntax (Toggle Plain Text)
Stream xmlFile = new FileStream(@"c:\path",FileMode.Append); XmlTextWriter textWritter = new XmlTextWriter(xmlFile, Encoding.Default);
Last edited by plazmo; Aug 25th, 2006 at 11:57 am.
Hi,
If you have a specific XML schema, you can use a DataSet not bound to a database or connection and provide your own schema. This way you can use all data bound controls and table cursors and at the end you can load/save the whole dataset back as XML.
Loren Soth
If you have a specific XML schema, you can use a DataSet not bound to a database or connection and provide your own schema. This way you can use all data bound controls and table cursors and at the end you can load/save the whole dataset back as XML.
Loren Soth
Best regards,
Loren Soth
Crimson K. Software _________________________________________________________________ Crimson K. Blog
Loren Soth
Crimson K. Software _________________________________________________________________ Crimson K. Blog
•
•
Join Date: Jun 2006
Posts: 3
Reputation:
Solved Threads: 0
First thanks all for your time & help.
I've found out another easier way to solve my problem.
About checking whether the file is already existed or not there is static method in "system.IO" called "File.Exist" ,which check whether te file is already existed or not.
About the appending Problem ,I've used "XmlDocument" class
inorder to use some methods in it .
The result'll be like that:
</USERS>
<User>
<UserName>Buggaya</UserName>
<Email>Buggaya@gmail.com</Email>
</User>
</USERS>
Here's my code :
I've found out another easier way to solve my problem.
About checking whether the file is already existed or not there is static method in "system.IO" called "File.Exist" ,which check whether te file is already existed or not.
About the appending Problem ,I've used "XmlDocument" class
inorder to use some methods in it .
The result'll be like that:
</USERS>
<User>
<UserName>Buggaya</UserName>
<Email>Buggaya@gmail.com</Email>
</User>
</USERS>
Here's my code :
if(!File.Exists("F:/Documents and Settings/Administrator/Desktop/Account.xml")) { XmlTextWriter textWritter=new XmlTextWriter("F:/Documents and Settings/Administrator/Desktop/Account.xml", null); textWritter.WriteStartDocument(); textWritter.WriteStartElement("USERS"); textWritter.WriteEndElement(); textWritter.Close(); } XmlDocument xmlDoc=new XmlDocument(); xmlDoc.Load("F:/Documents and Settings/Administrator/Desktop/Account.xml"); XmlElement subRoot=xmlDoc.CreateElement("User"); //UserName XmlElement appendedElementUsername=xmlDoc.CreateElement("UserName"); XmlText xmlTextUserName=xmlDoc.CreateTextNode(txtUsrName.Text.Trim()); appendedElementUsername.AppendChild(xmlTextUserName); subRoot.AppendChild(appendedElementUsername); xmlDoc.DocumentElement.AppendChild(subRoot); //Email XmlElement appendedElementEmail=xmlDoc.CreateElement("Email"); XmlText xmlTextEmail=xmlDoc.CreateTextNode(txtEmail.Text.Trim()); appendedElementEmail.AppendChild(xmlTextEmail); subRoot.AppendChild(appendedElementEmail); xmlDoc.DocumentElement.AppendChild(subRoot); xmlDoc.Save("F:/Documents and Settings/Administrator/Desktop/Account.xml");
![]() |
Similar Threads
- [urgent] HTML form => XML file (JSP)
- Parse an XML document with AJAX (JavaScript / DHTML / AJAX)
- Searching and Comparing strings from an XML Document (Python)
- Database table convert to xml document to cache (VB.NET)
- append to XML file (Visual Basic 4 / 5 / 6)
- problem reading xml file in c# (C#)
Other Threads in the C# Forum
- Previous Thread: Accesing controls on the form from custom method
- Next Thread: firewall application
| Thread Tools | Search this Thread |
.net access algorithm alignment array barchart bitmap box broadcast buttons c# c#gridviewcolumn check checkbox client combobox communication control conversion csharp custom database datagrid datagridview dataset datetime degrees development draganddrop drawing elevated encryption enum event excel file focus forloop form format forms function gdi+ hospitalmanagementsystem httpwebrequest image index input install java label list listbox localization login mandelbrot math messagebox mouseclick mysql operator path photoshop picturebox pixelinversion plotting pointer post programming radians read regex remote remoting richtextbox server sleep socket sql statistics stream string stringformatting sun table text textbox thread time timer update usercontrol validation visualstudio webbrowser whileloop windows winforms wpf xml





