Re: Appending to an XML file.

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



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


.



Relevant Pages

  • Re: How to control root node name for the DataTable.WriteXML method ca
    ... Robbe Morris - 2004/2005 Microsoft MVP C# ... > datatable as an XML file.. ... root node of the output file is always DocumentElement. ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: XML Logging Write one record
    ... The reason we like to log to an xml file is that we like to analyse this ... But i don't want read the complete file into memory for writing one record. ... complete XMLDocument that you add nodes to (as children of the root node at ... Appending to an XML file sequentially is problematic ...
    (microsoft.public.dotnet.languages.vb)
  • How to control root node name for the DataTable.WriteXML method
    ... I am trying to leverage ADO.NET 2.0 DataTable WriteXML method and serialize ... datatable as an XML file.. ... root node of the output file is always DocumentElement. ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: Find an xml node
    ... If this is a proper XML file it will have a root node these nodes will be children of. ... In that case you can use XDocument ) and XLINQ: ... "Marco Trapanese" wrote in message ...
    (microsoft.public.dotnet.languages.vb)
  • Can I use OODoc to create a paragraph with a mix of bold and normal text?
    ... I have a fairly complex Perl script that reads in data from an XML file ... I have found the documentation sparse and confusing and cannot fathom how to ... make it append formatted text to a paragraph. ... string in bold', 'append this string in italics', 'append this string as a ...
    (comp.lang.perl.misc)