Append in xml document

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Jun 2006
Posts: 3
Reputation: Buggaya is an unknown quantity at this point 
Solved Threads: 0
Buggaya Buggaya is offline Offline
Newbie Poster

Append in xml document

 
0
  #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();
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 206
Reputation: plazmo is an unknown quantity at this point 
Solved Threads: 16
plazmo's Avatar
plazmo plazmo is offline Offline
Posting Whiz in Training

Re: Append in xml document

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

Re: Append in xml document

 
0
  #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
Best regards,
Loren Soth

Crimson K. Software _________________________________________________________________ Crimson K. Blog
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 3
Reputation: Buggaya is an unknown quantity at this point 
Solved Threads: 0
Buggaya Buggaya is offline Offline
Newbie Poster

Thanks

 
0
  #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 Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC