473,394 Members | 1,740 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

how do i append to xml files

abehm
35
Hi,
I have a method that inserts class data into a new xml document, however I would like to append the data after the last element of an existing xml document.

here is the code I have:
if anyone can offer any help or suggestions, that would be appreciated.


Expand|Select|Wrap|Line Numbers
  1.     public string Insert(string XMLFile)
  2.     {
  3.         string XMLDirectory = this.xmlDir + this.ProjectorSN + @"\";
  4.         string file = XMLDirectory + XMLFile;
  5.         System.Xml.XmlTextWriter writer;
  6.  
  7.         if (!System.IO.Directory.Exists(XMLDirectory))
  8.         {
  9.             System.IO.Directory.CreateDirectory(XMLDirectory);  
  10.         }
  11.  
  12.         writer = new System.Xml.XmlTextWriter(file, System.Text.Encoding.UTF8);
  13.         writer.Formatting = System.Xml.Formatting.Indented;
  14.         writer.WriteStartDocument();
  15.         writer.WriteStartElement("ProductTest");
  16.  
  17.         writer.WriteElementString("ProductSN", this.ProjectorSN);
  18.         writer.WriteElementString("PackageID", this.LightEngineSN);
  19.         writer.WriteElementString("Part2", this.Lamp1);
  20.         writer.WriteElementString("Part3", this.Lamp2);
  21.         writer.WriteElementString("DateTime", this.DateTime);
  22.  
  23.         writer.WriteWhitespace(" ");
  24.         // Write End of root element
  25.         writer.WriteEndElement();
  26.  
  27.         // Write End of document
  28.         writer.WriteEndDocument();
  29.  
  30.         writer.Close();
  31.  
  32.         return (XMLDirectory + XMLFile + " saved.");
  33.     } 
May 18 '07 #1
3 1806
dorinbogdan
839 Expert 512MB
Welcome to TheScripts TSDN....

Try to create a new file stream and set the access to append.
Expand|Select|Wrap|Line Numbers
  1. Stream xmlFile = new FileStream(@"c:\path",FileMode.Append);
  2. XmlTextWriter textWritter = new XmlTextWriter(xmlFile, Encoding.Default);
  3. //author: plazmo 
May 18 '07 #2
abehm
35
Welcome to TheScripts TSDN....

Try to create a new file stream and set the access to append.
Expand|Select|Wrap|Line Numbers
  1. Stream xmlFile = new FileStream(@"c:\path",FileMode.Append);
  2. XmlTextWriter textWritter = new XmlTextWriter(xmlFile, Encoding.Default);
  3. //author: plazmo 
okay i tried that and it worked, only it appended after the endelement of the root.
how do i append just before that?
May 18 '07 #3
dorinbogdan
839 Expert 512MB
At this point I suggest you to try XMLDocument, it's more flexible.
If not sure how to use, see this link.
May 21 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: waters | last post by:
I seem to have hit a snag. I am trying to add the list (l_local) as an item in the output list (l_output). "l_local" is a modified copy of the format-list "l_format", which will be updated by...
1
by: David Barger | last post by:
Greetings, It appears that an Append Query I run in Access XP is randomly failing to append a field. I have payroll data being entered into a payroll database. This data is exported daily to...
8
by: WordVBAProgrammer | last post by:
I've been struggling with this for a few days now. It worked originally as plain VB-type strings, but for some reason ceased creating the FileNm. I changed the code to use StringBuilder, but...
3
by: Mohan | last post by:
Hi I face a problem with the stl string append on our 32-bit SLES9 machine . (It is not happening on SLES9 64-bit machines and on SuSE 10). When we read a 179MB file into the memory,...
12
by: vj | last post by:
I am using Borland C++. I run a program which generates output data files, say 'Voltage.dat', 'Current.dat'. I have a variable in my code (say N), and for various values of N (for each value of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.