469,352 Members | 1,892 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,352 developers. It's quick & easy.

How to append records in exist XMl file?

Greetings

I want to append records in a XML file,I use XMLTextWriter,but i found it
only create a XML file,not append records in exist XML file.why?
-------------------------------------------------
XmlTextWriter myXmlTextWrite = new XmlTextWriter(@"C:\myconfig.xml");
myXmlTextWrite.WriteStartDocument();
myXmlTextWrite.WriteStartElement("Record");
myXmlTextWrite.WriteStartElement("Topic");
myXmlTextWrite.WriteString(XMLTopic);
myXmlTextWrite.WriteEndElement();
myXmlTextWrite.WriteEndElement(); //end Record
myXmlTextWrite.WriteEndDocument();
myXmlTextWrite.Flush();
myXmlTextWrite.Close();
=================================================
i should use XMLDocument?
------------------------------
XmlDocument xmldoc= new XmlDocument();
xmldoc.Load(@"C:\myconfig.xml");
xmldoc.DocumentElement.AppendChild() //????????

Nov 12 '05 #1
2 12721
that's the whole idea (XmlTextWriter has to validate content as XML ...), so
using XmlDoxument makes sense ...
"Matrix" <co*****@163.com> wrote in message
news:eE****************@TK2MSFTNGP10.phx.gbl...
Greetings

I want to append records in a XML file,I use XMLTextWriter,but i found it
only create a XML file,not append records in exist XML file.why?
-------------------------------------------------
XmlTextWriter myXmlTextWrite = new XmlTextWriter(@"C:\myconfig.xml");
myXmlTextWrite.WriteStartDocument();
myXmlTextWrite.WriteStartElement("Record");
myXmlTextWrite.WriteStartElement("Topic");
myXmlTextWrite.WriteString(XMLTopic);
myXmlTextWrite.WriteEndElement();
myXmlTextWrite.WriteEndElement(); //end Record
myXmlTextWrite.WriteEndDocument();
myXmlTextWrite.Flush();
myXmlTextWrite.Close();
=================================================
i should use XMLDocument?
------------------------------
XmlDocument xmldoc= new XmlDocument();
xmldoc.Load(@"C:\myconfig.xml");
xmldoc.DocumentElement.AppendChild() //????????

Nov 12 '05 #2
Matrix wrote:
I want to append records in a XML file,I use XMLTextWriter,but i found it
only create a XML file,not append records in exist XML file.why?


XML document is tree-like structure, so to append records one has to
rearrange structure, what hardly possible in file on disk.
The only case when XmlWriter can append data to XML file is when it's
not XML document, but XML fragment (XML document with more than single
root element). That's common pattern for writing XML-based log files. As
there is no single root element rule, you can merely append records to
the end of file:

<log time="..." event="..."/>
<log time="..." event="..."/>
<log time="..." event="..."/>

Above is XML fragment, which is easily appendable and can be incpropated
into another document via external entity or just read using
XmlTextReader, which happily supports reading of XML fragments.
See sample at http://www.tkachenko.com/blog/archives/000053.html

--
Oleg Tkachenko [XML MVP, XmlInsider]
http://blog.tkachenko.com
Nov 12 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by JMCN | last post: by
3 posts views Thread by Larry Rekow | last post: by
1 post views Thread by Matrix | last post: by
5 posts views Thread by solar | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.