XmlWriter将大型xml文件创建分为方法

时间:2017-06-27 18:10:41

标签: c# .net xml xmlwriter

我正在使用XmlWriter创建一个大型xml文件,我知道使用

             using (XmlWriter writer = XmlWriter.Create("file.xml", settings))
        {

            writer.WriteStartElement("Research", "http://www.rixml.org/2013/2/RIXML");
            writer.WriteAttributeString("xmlns", "rixmldt", null, "http://www.rixml.org/2013/2/RIXML-datatypes");
            writer.WriteAttributeString("xsi", "schemaLocation", null, "http://www.rixml.org/2013/2/RIXML");
            //dropdown selection for reseach id?
            writer.WriteAttributeString("researchID", "BOGUS ID");
            writer.WriteAttributeString("language", "eng");
            //fix date time
            writer.WriteAttributeString("createdDateTime", System.DateTime.Now.Date.ToString());
            writer.WriteAttributeString("xmlns", "xsi", null, "http://www.w3.org/2001/XMLSchema-instance");
            writer.WriteStartElement("Product");
            //dropdown selection for prioduct id?
            writer.WriteAttributeString("productID", "asdf");
            //status info
            writer.WriteStartElement("StatusInfo");

            writer.WriteAttributeString("currentStatusIndicatior", "Yes");
            writer.WriteAttributeString("statusDateTime", System.DateTime.Now.Date.ToString());
            writer.WriteAttributeString("statusType", "Published");
            writer.WriteEndElement();
            writer.WriteStartElement("Source");
            writer.WriteStartElement("Organization");
            //organization info
            writer.WriteAttributeString("type", "SellSideFirm");
            writer.WriteAttributeString("primaryIndicatior", "Yes");
            //organization1
            writer.WriteStartElement("OrganizationID");
            writer.WriteAttributeString("idType", "Bloomberg");
            writer.WriteString("1234");
            writer.WriteEndElement();
            //org 2
            writer.WriteStartElement("OrganizationID");
            writer.WriteAttributeString("idType", "FactSet");
            writer.WriteString("rep_example");
            writer.WriteEndElement();
....
            writer.WriteEndElement();
            writer.Flush();
}

被认为是生成大文件的最快方法,但我似乎无法找到任何人(或在XmlWriter文档中)将其全部分解为生成xml块的方法。我的方法大约是150行,并且我希望尽可能保持模块化,因为所有正在填充的数据将主要从表单中提取。这意味着此声明所使用的方法最终也会产生大量参数。

为了模块化/组织起见,在不使用文件流或在本地保存数据的情况下是这样的吗?或者它最终会放慢速度?

1 个答案:

答案 0 :(得分:1)

我喜欢下面的代码比你好一点,因为它更有条理,更易于阅读,而且你不必添加结束元素

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;

            string header = "<Research xmlns=\"http://www.rixml.org/2013/2/RIXML\"" +
                                              " xmlns:rixmldt=\"http://www.rixml.org/2013/2/RIXML-datatypes\"" +
                                              " xmlns:xsi=\"http://www.rixml.org/2013/2/RIXML\"" +
                                              " researchID=\"BOGUS ID\"" +
                                              " language=\"eng\"" +
                                              " createdDateTime=\"" + System.DateTime.Now.Date.ToString() + "\"" +
                                              " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">";


            using (XmlWriter writer = XmlWriter.Create(FILENAME, settings))
            {
                writer.WriteRaw(header) ;

                XElement product = new XElement("Product", new XAttribute("productID", "asdf"));

                XElement statusInfo = new XElement("StatusInfo", new object[] {
                        new XAttribute("currentStatusIndicatior", "Yes"),
                        new XAttribute("statusDateTime", System.DateTime.Now.Date.ToString()),
                        new XAttribute("statusType", "Published")
                });
                product.Add(statusInfo);

                XElement source = new XElement("Source", new object[] {
                    new XElement("Organization", new object[] {
                        new XAttribute("type", "SellSideFirm"),
                        new XAttribute("primaryIndicatior", "Yes"),
                        new XElement("OrganizationID", new object[] {
                            new XAttribute("idType", "Bloomberg"),
                            "1234"
                        })
                    })
                });
                product.Add(source);

                XElement organizationID = new XElement("OrganizationID", new object[] {
                    new XAttribute("idType", "FactSet"),
                    "rep_example"
                });
                product.Add(organizationID);

                product.WriteTo(writer);

                writer.Flush();
                writer.Close();
            }
        }
    }
}