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();

Recommended Answers

All 5 Replies

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);

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

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");

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");


        }
        string title = Title.Text;
        string authur = Author.Text;
        string isbn = ISBN.Text;
        string published = Published.Text;


        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(path);
        XmlElement subRoot = xmlDoc.CreateElement("book");
        XmlElement etitle = xmlDoc.CreateElement("Title");
        XmlElement eauthor = xmlDoc.CreateElement("Author");
        XmlElement eisbn = xmlDoc.CreateElement("ISBN");
        XmlElement epublished = xmlDoc.CreateElement("Published");
        etitle.InnerText = title;
        eauthor.InnerText = authur;
        eisbn.InnerText = isbn;
        epublished.InnerText = published;

        subRoot.AppendChild(etitle);
        subRoot.AppendChild(eauthor);
        subRoot.AppendChild(eisbn);
        subRoot.AppendChild(epublished);
        xmlDoc.DocumentElement.AppendChild(subRoot);

        xmlDoc.Save(path);
XML Looks like this
        <?xml version="1.0" encoding="utf-8"?>
<lib>
  <book>
    <Title>Pro XML</Title>
    <Author>Behrang</Author>
    <ISBN>2001509</ISBN>
    <Published>11/30/2012</Published>
  </book>
</lib>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.