正如标题所示,我需要将日志数据追加到XML文件而不缓冲到RAM。 XML文件由LogEntry元素组成,其中包含82个包含数据的子元素。这些文件可能会变得非常大,因为它会构成Windows CE6程序的一部分,所以我们的内存非常有限。将元素复制并附加到XML文档而不缓冲到RAM
做完的研究有相当数量是明显的,最常用的方法是使用XDocument
或Linq to XML
追加到它,写出来的新文档之前已有的文件中读取。一起使用XmlWriter
和XmlReader
似乎是我追加到文件的最好方式,但到目前为止,我所有的尝试都是非常不切实际的,并且需要IF语句来指示要写入的内容,以防止重复或数据较少的元素被写入。
的我在做什么的本质是:
//Create an XmlReader to read current WorkLog.
using (XmlReader xmlRead = XmlTextReader.Create("WorkLog.xml"))
{
//Create a XmlWriterSettings and set indent
//to true to correctly format the document
XmlWriterSettings writerSettings = new XmlWriterSettings();
writerSettings.Indent = true;
writerSettings.IndentChars = "\t";
//Create a new XmlWriter to output to
using (XmlWriter xmlWriter = XmlWriter.Create("New.xml", writerSettings))
{
//Starts the document
xmlWriter.WriteStartDocument();
//While the XmlReader is still reading (essentially !EOF)
while (xmlRead.Read())
{
//FSM to direct writing of OLD Log data to new file
switch (xmlRead.NodeType)
{
case XmlNodeType.Element:
//Handle the copying of an element node
//Contains many if statements to handle root node &
//attributes and to skip nodes that contain text
break;
case XmlNodeType.Text:
//Handle the copying of an text node
break;
case XmlNodeType.EndElement:
//Handle the copying of an End Element node
break;
}
}
xmlWriter.WriteEndDocument();
}
}
我相信我可以这样追加到该文件,但它是非常不切实际的这样做 - 没有人知道任何内存有效的方法我的搜寻时间没有变成什么?
如果需要,我很高兴发布我的当前代码来做到这一点 - 但正如我所说,它是非常大的,现在实际上非常讨厌,所以我现在就离开它。
处理XML文件作为文本比阅读整个事情,然后解析它更为高效和显著快,只需添加新数据到最后.... – 2013-04-30 05:22:54