如何写(大)XML在C#中的文件? [英] How to write (big) XML to a file in C#?

查看:80
本文介绍了如何写(大)XML在C#中的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

伙计们,

请,什么是写非常大的XML文档的一个很好的方式(高达500说MB)的C#.NET 3.5? 。我有一点搜索周围,似乎并不能找到任何这解决了这一具体问题。

Please, what's a good way of writing really big XML documents (upto say 500 MB) in C# .NET 3.5? I've had a bit of search around, and can't seem to find anything which addresses this specific question.

我以前的线程(的http://stackoverflow.com/questions/676274/what-is -the-最佳的方式到语法分析大的XML-在C代码)涵盖阅读类似幅度的XML文档...随着上解决了我需要思考如何写更新的功能(< A HREF =http://www.opengeospatial.org/standards/sfa> http://www.opengeospatial.org/standards/sfa )到update.xml的文件。

My previous thread (http://stackoverflow.com/questions/676274/what-is-the-best-way-to-parse-big-xml-in-c-code) covered reading similar magnitude Xml documents... With that solved I need to think about how to write the updated features (http://www.opengeospatial.org/standards/sfa) to an "update.xml" document.

我的思路:显然,一个大的DOM出来了,考虑到生产的文件的最大尺寸。我使用XSD.EXE生成绑定类形成的模式...这与XmlSerializer类的作品很好,但我认为它建立引擎盖下一个DOM。它是否正确?。我不能在同一时间保留在内存中的所有功能(高达50000人)。我需要阅读功能,形成数据库,序列化,并将其写入到文件。所以我想我应该用XmlSerializer的写为每个单独的功能,将文件的doclet。我有不知道(但)如果这甚至有可能/可行的。

My ideas: Obviously one big DOM is out, considering the maximum size of the document to be produced. I'm using XSD.EXE to generate binding classes form the schema... which works nicely with the XmlSerializer class, but I think it builds a DOM "under the hood". Is this correct?. I can't hold all the features (upto 50,000 of them) in memory at one time. I need to read a feature form the database, serialize it, and write it to file. So I'm thinking I should use the XmlSerializer to write a "doclet" for each individual feature to the file. I've got no idea (yet) if this is even possible/feasible.

你觉得呢?

背景:我移植一个老VB6 MapInfo的客户端插件为C#。有一个现有的J2EE升级服务(实际上只是一个web应用程序),它这个程序(其中包括)必须工作。我不能更改服务器;除非absapositively必要的;特别的,涉及改变其他客户端。服务器接受与模式不specificy任何命名空间...即一个XML文档:只有默认的命名空间,并且一切都在它

Background: I'm porting an old VB6 MapInfo "client plugin" to C#. There is an existing J2EE "update service" (actually just a web-app) which this program (among others) must work with. I can't change the server; unless absapositively necessary; especially of that involves changing the other clients. The server accepts an XML document with a schema which does not specificy any namespaces... ie: there is only default namespace, and everything is in it.

我的经验:我是一个很值得C#和.NET新手。我在各种语言包括Java,VB,C和一些C ++编程了约10一年。

My experience: I'm pretty much a C# and .NET newbie. I've been programming for about 10 year in various languages including Java, VB, C, and some C++.

干杯所有。基思

PS:这是吃饭时间,所以我会约一个半小时是AWOL

PS: It's dinner time, so I'll be AWOL for about half an hour.

推荐答案

有关编写大型XML,的XmlWriter (直接)是你的朋友 - 但它是很难使用。另一种选择是使用DOM /对象模型的方法,并结合他们,这可能是可行的如果的你抓住的 XmlWriterSettings 控制和禁用在XML标记,并开始使用系统摆脱了命名空间声明的......

For writing large xml, XmlWriter (directly) is your friend - but it is harder to use. The other option would be to use DOM/object-model approaches and combine them, which is probably doable if you seize control of the XmlWriterSettings and disable the xml marker, and get rid of the namespace declarations...

using System;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Serialization;    
public class Foo {
    [XmlAttribute]
    public int Id { get; set; }
    public string Bar { get; set; }
}
static class Program {
    [STAThread]
    static void Main() {
        using (XmlWriter xw = XmlWriter.Create("out.xml")) {
            xw.WriteStartElement("xml");
            XmlSerializer ser = new XmlSerializer(typeof(Foo));
            XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
            ns.Add("","");
            foreach (Foo foo in FooGenerator()) {
                ser.Serialize(xw, foo, ns);
            }
            xw.WriteEndElement();
        }
    }    
    // streaming approach; only have the smallest amount of program
    // data in memory at once - in this case, only a single `Foo` is
    // ever in use at a time
    static IEnumerable<Foo> FooGenerator() {
        for (int i = 0; i < 40; i++) {
            yield return new Foo { Id = i, Bar = "Foo " + i };
        }
    }
}

这篇关于如何写(大)XML在C#中的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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