在C#中,有没有办法添加到磁盘上的文件的XML节点不先加载它? [英] In C#, is there a way to add an XML node to a file on disk WITHOUT loading it first?

查看:76
本文介绍了在C#中,有没有办法添加到磁盘上的文件的XML节点不先加载它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有磁盘上的一个非常基本的XML结构/文件,该文件是这样的:

I have a very basic XML structure/file on disk which is something like:

<root>
    <data timestamp="dd-mm-yyyy" type="comment">kdkdkdk</data>
    <data timestamp="dd-mm-yyyy" type="event">kdkdkdkgffgfdgf</data>
    <data timestamp="dd-mm-yyyy" type="something">kddsdsfsdkdkdk</data>
</root>



XML将是,在磁盘上的一个外部文件中提及。由于文件可能增长相当大(实际上是被修剪每隔几周),我不想先加载XML文件,添加一个新的节点......

The XML will be, as mentioned, in an external file on disk. As the file might grow fairly large (actually gets 'trimmed' every couple of weeks), I don't want to load the XML file first to add a new node...

有没有一种方法来添加一个新的节点也是这样吗?可刚添加到顶部/底部等,因为这实际上是由时间戳使用XML种类也无妨。

Is there a way to add a new node like this? it can be just added to the top/bottom etc, as the process that actually uses the XML sorts it by timestamp anyway..

我猜一个粗暴的方式是追加该节点为文本。但是我认为这将添加节点结束标记之后??

I'm guessing a crude way is to append the node as text.. however I think that would add the node AFTER the end tag??

任何想法感激地接受..
大卫。

Any ideas gratefully received.. David.

推荐答案

有像样的答案AA号码已经在这里,但只是把另一旋转这个,你确定这个数据需要存储为一个XML文件?

There are a a number of decent answers here already, however just to put another spin on this, are you sure this data needs to be stored as an XML file?

你是存储看起来非常简单的数据(即三个字段,日期,类型和一些字符串数据的记录。如果不这样做需要任何额外的好处XML提供(看到这里 ),那么为什么不直接使用基本CSV?这样,你可以只保留追加到它,就像通过日志文件File.AppendText(文件名)

The data you are storing looks fairly simple (i.e a record with three fields, date, type and some string data. If you do not need any of the added benefits that XML provides (see here) then why not just use basic CSV? That way you can just keep appending to it just like a log file via File.AppendText("filename").

(地狱,它甚至有可能使用像log4net的管理记录和任何清理家务。)

(Hell, it might even be possible to use something like log4net to manage the logging and any clean up housekeeping.)

这篇关于在C#中,有没有办法添加到磁盘上的文件的XML节点不先加载它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆