954,529 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Append in xml document

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();
Buggaya
Newbie Poster
3 posts since Jun 2006
Reputation Points: 10
Solved Threads: 0
 

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);
plazmo
Posting Whiz in Training
207 posts since Aug 2005
Reputation Points: 23
Solved Threads: 16
 

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

Lord Soth
Posting Whiz in Training
233 posts since Mar 2006
Reputation Points: 28
Solved Threads: 4
 

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:

BuggayaBuggaya@gmail.com




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");
Buggaya
Newbie Poster
3 posts since Jun 2006
Reputation Points: 10
Solved Threads: 0
 

I have four text boxes,
txtISBN.Text = string.Empty;
txtBookName.Text = string.Empty;
txtPublication.Text = string.Empty;
txtPublishYear.Text = string.Empty;
and File path

string sStartupPath = Application.StartupPath;

After filling all text boxes when you will click on Create XML Doc button, it will check if xml file is not exist than it will create an xml file and add element into it.

In this sample I'm not validating any data.

private void btnCreateXMLDoc_Click(object sender, EventArgs e)
{

AddBook(txtISBN.Text, txtBookName.Text, txtPublication.Text, txtPublishYear.Text);


}

private void AddBook(string ISBN, string Title, string Publisher, string PublishYear)
{
XmlElement booksElement = null ;
if (!File.Exists(sStartupPath + @"\Books.xml"))
{

if (booksElement == null)
{
booksElement = xmlDoc.CreateElement("Books");
xmlDoc.AppendChild(booksElement);
xmlDoc.Save(sStartupPath + @"\Books.xml");
}
}
else
{
xmlDoc.Load(sStartupPath + @"\Books.xml");
booksElement = xmlDoc.DocumentElement; //getting the root element name
}

XmlElement bookElement = xmlDoc.CreateElement("Book");

//Attributes are extra information added to a node element itself
XmlAttribute bookAttribute = xmlDoc.CreateAttribute("ISBN");
bookElement.SetAttributeNode(bookAttribute);
bookAttribute.Value = ISBN.Trim();

XmlElement titleElement = xmlDoc.CreateElement("Title");
titleElement.InnerText = Title.Trim();
bookElement.AppendChild(titleElement);

XmlElement releaseElement = xmlDoc.CreateElement("Release");
releaseElement.InnerText = PublishYear.Trim();
bookElement.AppendChild(releaseElement);

XmlElement publisherElement = xmlDoc.CreateElement("Publisher");
publisherElement.InnerText = Publisher.Trim();
bookElement.AppendChild(publisherElement);

booksElement.AppendChild(bookElement);
xmlDoc.Save(sStartupPath + @"\Books.xml");

}

C K Wadhwa
Newbie Poster
1 post since Jul 2011
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You