Re: Appending to an XML file.
- From: "Nicholas Paldino [.NET/C# MVP]" <mvp@xxxxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Tue, 25 Oct 2005 10:45:19 -0400
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]
- mvp@xxxxxxxxxxxxxxxxxxxxxxxxxxx
<tony.collings@xxxxxxxxx> wrote in message
news:1130246259.948305.74040@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
>I started a thread here :
> http://groups.google.co.uk/group/microsoft.public.cmserver.general/browse_thread/thread/29d63077144004a9/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");
> }
>
>
> }
>
.
- References:
- Appending to an XML file.
- From: tony . collings
- Appending to an XML file.
- Prev by Date: Logging into a network via C#
- Next by Date: Re: How to understand the use of namespaces ...
- Previous by thread: Re: Appending to an XML file.
- Next by thread: Re: Invoke
- Index(es):
Relevant Pages
|