How to write (big) XML to a file in C#?

前端 未结 3 472
心在旅途
心在旅途 2021-02-04 09:39

Folks,

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 anythin

3条回答
  •  天命终不由人
    2021-02-04 09:43

    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 theXmlWriterSettings 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 FooGenerator() {
            for (int i = 0; i < 40; i++) {
                yield return new Foo { Id = i, Bar = "Foo " + i };
            }
        }
    }
    

提交回复
热议问题