473,465 Members | 1,373 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Appending to an XML file.

I started a thread here :
http://groups.google.co.uk/group/mic...888efdcb7338f6

About creating an Email function to email authors when the Review Date
has expired on their page.

I've managed to now achieve a significant proportion of the work by
writing out the details to an XML file and reading them back.

However I'm a little stuck on Amending an XML file. At the moment I'm
deleting it and recreating, but I need to append to it instead.

I was reading up on the DiffGram method of WriteXml. Anyone got any
pointers ?

Code run at PageLoad
private void ReadWriteXMLDocumentWithXMLReader()
{
// TODO:: Dynamically create the flag, and then append to XML
// Create a DataSet with one table and two columns.
DataSet EmailSentDataSet = new DataSet("EmailSentDataSet");
DataTable EmailSentTable = new DataTable("EmailSentTable");

DataColumn c1 = new DataColumn("url");
DataColumn c2 = new DataColumn("flag",
Type.GetType("System.Int32"));
EmailSentTable.Columns.Add(c1);
EmailSentTable.Columns.Add(c2);
EmailSentDataSet.Tables.Add(EmailSentTable);

DataRow newRow;
{
newRow = EmailSentTable.NewRow();
newRow["url"]= currentContext.Posting.Url.ToString();
newRow["flag"] = true;
EmailSentTable.Rows.Add(newRow);
}
EmailSentDataSet.AcceptChanges();

//Write XML File.
string xmlFilename = @"C:\emailflags.xml";

System.IO.FileStream WriteXml = new System.IO.FileStream
(xmlFilename, System.IO.FileMode.Create);

System.Xml.XmlTextWriter xmlWriter = new System.Xml.XmlTextWriter
(WriteXml, System.Text.Encoding.Unicode);

EmailSentDataSet.WriteXml(xmlWriter);
WriteXml.Close();
//EmailSentDataSet.Dispose();

//Read Data Back In
DataSet newDataSet = new DataSet("New DataSet");
System.IO.FileStream fsReadXml = new System.IO.FileStream
(xmlFilename, System.IO.FileMode.Open);
System.Xml.XmlTextReader myXmlReader =
new System.Xml.XmlTextReader(fsReadXml);
newDataSet.ReadXml(myXmlReader);
myXmlReader.Close();

string url;
if(newDataSet.Tables[0].Rows.Count > 0)
{
url = newDataSet.Tables[0].Rows[0]["url"].ToString();
Trace.Warn (url);
}
else
{
Trace.Warn("Nothing in the Dataset");
}
}

Nov 17 '05 #1
2 2214
to***********@gmail.com wrote:

<snip>
I've managed to now achieve a significant proportion of the work by
writing out the details to an XML file and reading them back.

However I'm a little stuck on Amending an XML file. At the moment I'm
deleting it and recreating, but I need to append to it instead.


You can't - at least, you can't append something to an XML document and
expect to get a valid XML document out at the end. A valid XML document
has exactly one root element - add anything to the end and you break
that.

Jon

Nov 17 '05 #2
Tony,

With the XML file, you will have to store the results in a file that is
not well-formed XML. Well-formed XML has a root node. If you keep the root
node in the file, then you will have to parse the whole file any time you
want to append to it (through the document object model). This can become
very time consuming as the file grows larger and larger.

However, if you eliminate the root node, then you can just open the file
for appending, and write the xml directly. The problem with this is that
when you want to read the information, you end up having to add the start
and end tags (which can be a time consuming process).

If the size becomes too large, you might want to consider moving to a
db.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<to***********@gmail.com> wrote in message
news:11*********************@g44g2000cwa.googlegro ups.com...
I started a thread here :
http://groups.google.co.uk/group/mic...888efdcb7338f6

About creating an Email function to email authors when the Review Date
has expired on their page.

I've managed to now achieve a significant proportion of the work by
writing out the details to an XML file and reading them back.

However I'm a little stuck on Amending an XML file. At the moment I'm
deleting it and recreating, but I need to append to it instead.

I was reading up on the DiffGram method of WriteXml. Anyone got any
pointers ?

Code run at PageLoad
private void ReadWriteXMLDocumentWithXMLReader()
{
// TODO:: Dynamically create the flag, and then append to XML
// Create a DataSet with one table and two columns.
DataSet EmailSentDataSet = new DataSet("EmailSentDataSet");
DataTable EmailSentTable = new DataTable("EmailSentTable");

DataColumn c1 = new DataColumn("url");
DataColumn c2 = new DataColumn("flag",
Type.GetType("System.Int32"));
EmailSentTable.Columns.Add(c1);
EmailSentTable.Columns.Add(c2);
EmailSentDataSet.Tables.Add(EmailSentTable);

DataRow newRow;
{
newRow = EmailSentTable.NewRow();
newRow["url"]= currentContext.Posting.Url.ToString();
newRow["flag"] = true;
EmailSentTable.Rows.Add(newRow);
}
EmailSentDataSet.AcceptChanges();

//Write XML File.
string xmlFilename = @"C:\emailflags.xml";

System.IO.FileStream WriteXml = new System.IO.FileStream
(xmlFilename, System.IO.FileMode.Create);

System.Xml.XmlTextWriter xmlWriter = new System.Xml.XmlTextWriter
(WriteXml, System.Text.Encoding.Unicode);

EmailSentDataSet.WriteXml(xmlWriter);
WriteXml.Close();
//EmailSentDataSet.Dispose();

//Read Data Back In
DataSet newDataSet = new DataSet("New DataSet");
System.IO.FileStream fsReadXml = new System.IO.FileStream
(xmlFilename, System.IO.FileMode.Open);
System.Xml.XmlTextReader myXmlReader =
new System.Xml.XmlTextReader(fsReadXml);
newDataSet.ReadXml(myXmlReader);
myXmlReader.Close();

string url;
if(newDataSet.Tables[0].Rows.Count > 0)
{
url = newDataSet.Tables[0].Rows[0]["url"].ToString();
Trace.Warn (url);
}
else
{
Trace.Warn("Nothing in the Dataset");
}
}

Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Thomas Heller | last post by:
I want to append/insert additional data to an xml file. Context: I use gccxml to parse C header files. gccxml creates an xml file containing all the definitions from the header files. The xml...
1
by: dmiller23462 | last post by:
Hey guys.... I put an error-handling in my page and have it posted at the complete end of the code, see below(when people were putting in 's I was getting the delimiter errors). Great, I...
7
by: Don | last post by:
Hi all, With regards to the following, how do I append the datetimestamp to the filenames in the form? The files are processed using the PHP script that follows below. Thanks in advance,...
1
by: Jonathan Taylor | last post by:
I have a large XML file, that is too large to read in to XmlDocument. I need to append data to this XML file without creating a new file, since I don't want to have two copies of the large file...
16
by: Michael | last post by:
I have a data application in a2k that I need to create two fixed width text files and then combine them to a single file The first file is header information and the second is transaction data. ...
4
by: jasper | last post by:
How can this be done? Thanks
1
by: Frank | last post by:
Hi, Let's say I have a file named myFile.xml Within that file I have blocks of data which I'd like to add at different times during the day. e.g. <LogEntry>
2
by: sarada purkait | last post by:
hii i have to write into a file from the start and then go on appending to it .. i tried using ( ios::out|ios::app) but by this the file keeps on appending every time i run the program and the...
1
by: ofuuzo1 | last post by:
Hi, Is there anyway I can append a new element to an existing xml without first loading the existing file into a variable, adding the new element into the variable and saving it by overwriting the...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.