User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C# section within the Software Development category of DaniWeb, a massive community of 425,926 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 1,686 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C# advertiser: Programming Forums
Views: 11684 | Replies: 3
Reply
Join Date: Jun 2006
Posts: 3
Reputation: Buggaya is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Buggaya Buggaya is offline Offline
Newbie Poster

Append in xml document

  #1  
Aug 24th, 2006
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":

 
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();
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2005
Location: Ohio
Posts: 204
Reputation: plazmo is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 16
plazmo's Avatar
plazmo plazmo is offline Offline
Posting Whiz in Training

Re: Append in xml document

  #2  
Aug 25th, 2006
try this.
it creates its own file stream and sets the access to append.
it replaces your fiest line
        Stream xmlFile = new FileStream(@"c:\path",FileMode.Append);
        XmlTextWriter textWritter = new XmlTextWriter(xmlFile, Encoding.Default);
Last edited by plazmo : Aug 25th, 2006 at 10:57 am.
Reply With Quote  
Join Date: Mar 2006
Posts: 219
Reputation: Lord Soth is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 3
Lord Soth's Avatar
Lord Soth Lord Soth is offline Offline
Posting Whiz in Training

Re: Append in xml document

  #3  
Aug 28th, 2006
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
Crimson K. Software _________________________________________________________________ Crimson K. Blog
Reply With Quote  
Join Date: Jun 2006
Posts: 3
Reputation: Buggaya is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Buggaya Buggaya is offline Offline
Newbie Poster

Thanks

  #4  
Aug 29th, 2006
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 :

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");
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C# Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C# Forum

All times are GMT -4. The time now is 9:03 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC