By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,028 Members | 994 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,028 IT Pros & Developers. It's quick & easy.

Appending to an XML file.

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.