Appending to an XML file.

T

tony.collings

I started a thread here :
http://groups.google.co.uk/group/mi...004a9/c3888efdcb7338f6?hl=en#c3888efdcb7338f6

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


}
 
J

Jon Skeet [C# MVP]

(e-mail address removed) wrote:

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
 
N

Nicholas Paldino [.NET/C# MVP]

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]
- (e-mail address removed)

I started a thread here :
http://groups.google.co.uk/group/mi...004a9/c3888efdcb7338f6?hl=en#c3888efdcb7338f6

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


}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top