直接写入XML到磁盘和追加要素 [英] Write XML directly to disk and append elements

查看:148
本文介绍了直接写入XML到磁盘和追加要素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图写一个XML文件,但它是太大的内存来存储,所以我想直接写入到磁盘。我一直在使用的XmlWriter尝试,但没有的功能,使我能够追加到文件的末尾,因此我愿意诉诸使用一个普通文件作家编写XML原料。

I am trying to write an XML file but it is too large to store in memory, thus I want to write to it directly to disk. I have tried using XmlWriter but there is not functionality to enable me to append to the end of the file, hence I am willing to resort to writing the XML raw using a regular file writer.

有谁知道的任何文件写作课,使我写直盘和使我改写里面的文件位置?

Does anyone know of any file writing classes that enable me to write straight to disk and which enable me to overwrite positions inside the file?

原因是,我需要能够写在根元素,这样我可以追加另外一个信息位收盘,还能够在需要的时候读取XML文件。举例来说,如果我有以下XML:

The reason is that I need to be able to write over the closing of the root element so that I may append another bit of information, but also be able to read the XML file when needed. For example, if I had the following XML:

<elements>
  <element>
  </element>
</elements>

如果我想读这一点,我可以,但如果我想写信给它,我必须先删除< /元素方式> 标记,添加其他元素,然后再追加结束标记

If I wanted to read this, I could, but if I want to write to it I must first delete the </elements> tag, append another element, and then append the closing tag again.

谢谢任何帮助。

推荐答案

您的可以:使用一个XmlTextWriter。

You can use an XmlTextWriter.

只要打开写入文件,寻找回结束元素的开始,然后追加要与XmlTextWriter的任何新的元素。要关闭文件,只需写的结束元素的原始文本,以使文档完整,就大功告成了。

Just open the file for writing, seek back to the start of the end element, and then append any new elements you want with the XmlTextWriter. To close the file, simply write the raw text for the end element to make the document complete and you're done.

下面是一个快速和肮脏的例子。

Here's a quick and dirty example.

与XML这样开始:

<?xml version="1.0" encoding="utf-8"?>
<DocumentElement>
    <FirstElem/>
</DocumentElement>

您可以打开它,并追加一个元素是这样的:

You can open it and append an element like this:

using (FileStream f = new FileStream(@"D:\a.xml", FileMode.OpenOrCreate, FileAccess.Write))
{
    f.Seek(-("</DocumentElement>\n".Length), SeekOrigin.End);
    using (XmlTextWriter x = new XmlTextWriter(f, Encoding.UTF8))
    {
        x.WriteStartElement("Another");
        x.WriteAttributeString("attr", "value");
        x.WriteEndElement();

        // Close the file with a new terminating end-element
        x.WriteRaw("\r\n</DocumentElement>\r\n");
    }
}

和结果是:

<?xml version="1.0" encoding="utf-8"?>
<DocumentElement>
    <FirstElem/>
<Another attr="value" />
</DocumentElement>

您可能无法获得完美的压痕等,但它是有效的XML。这也正是如果编写XML作为原始文本文件,你会怎么做 - 但你不妨利用XML作家做的格式为你

You may not get the indentation perfect etc, but it's valid XML. This is exactly what you'd do if writing xml as raw text to the file - but you might as well leverage the XML writer to do the formatting for you.

我倒是还与一些评论同意 - 这将是非常有益的使用模式为XML的尺寸最小化。关闭缩进。用最短的元素和属性名就可以了。如果您正在使用叶元素,存储数据的属性,而不是CDATA将节省机房(<组件>数据< /组件> 更贵比<元素VAL =数据/> ,这可以进一步被压缩< EV =数据/> - 几乎一半的原始大小)

I'd also agree with some of the comments - it will be very beneficial to use a schema for your xml that minimises the size. Turn off indentation. Use the shortest element and attribute names you can. And if you are working on leaf elements, storing data as attributes rather than cdata will save room (<element>data</element> is more expensive than <element val="data"/> and this can be compressed further to <e v="data"/> - almost half the original size)

这篇关于直接写入XML到磁盘和追加要素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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