簡體   English   中英

XmlWriter大型xml文件創建分為方法

[英]XmlWriter large xml file creation divided into methods

我正在使用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行,我想盡可能地保持模塊化,因為要填充的所有數據大部分都會從表單中提取。 這意味着該語句所使用的方法最終還將包含大量的參數。

那么,出於模塊化/組織的目的,在不使用文件流或不將數據保存在本地的情況下,是否可能發生這種情況? 還是會最終減慢速度?

我喜歡下面的代碼,它比您的代碼要好一些,因為它組織得更好,更易於閱讀,而且您不必添加結束元素

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();
            }
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM