943,166 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 43982
  • C# RSS
Aug 24th, 2006
0

Append in xml document

Expand Post »
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();
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Buggaya is offline Offline
3 posts
since Jun 2006
Aug 25th, 2006
0

Re: Append in xml document

try this.
it creates its own file stream and sets the access to append.
it replaces your fiest line
C# Syntax (Toggle Plain Text)
  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.
Reputation Points: 23
Solved Threads: 16
Posting Whiz in Training
plazmo is offline Offline
206 posts
since Aug 2005
Aug 28th, 2006
0

Re: Append in xml document

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
Reputation Points: 28
Solved Threads: 4
Posting Whiz in Training
Lord Soth is offline Offline
233 posts
since Mar 2006
Aug 29th, 2006
0

Thanks

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");
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Buggaya is offline Offline
3 posts
since Jun 2006
Jul 17th, 2011
0
Re: Append in xml document
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");


}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
C K Wadhwa is offline Offline
1 posts
since Jul 2011

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Create pdf file from c#
Next Thread in C# Forum Timeline: Converting number to words(spell) in C Sharp [C#]





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC