Wednesday

split large xml file in c#

      
using Microsoft.XLANGs.BaseTypes;
using MyWebComponents;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Xml;

[Serializable]
public class MasterProductSplitter
{
    public MasterProductSplitter()
    {
    }

    List<string> ops = new List<string>();

    const string schema = "http://schemas.datacontract.org/2004/07/MyWebACPRecords";
    const string RecordTag = "b:getACPMasterRecords_Result";

    public void Dispose()
    {
        Thread.Sleep(5000);//let's wait until all files closes.
        foreach (var s in ops)
        {
            try
            {
                File.Delete(s);
            }
            catch (Exception ex)
            {
                LogError("Error deleting file :", ex);
            }

        }

    }

    public void InitialiseMsg(XLANGMessage message, DateTime RequestTime)
    {
        Initialise((Stream)message[0].RetrieveAs(typeof(Stream)), RequestTime);
    }

    public int Length { get { return ops.Count; } }
    public int Current;

    public const int MaxCount = 1000;

    public void Initialise(Stream ins, DateTime RequestTime)
    {
        Debug.Write("MasterProductSplitter v1.1 started.");
        ins.Seek(0, SeekOrigin.Begin);
        ops = new List<string>();
        Current = 0;

        XmlTextReader doc = new XmlTextReader(ins);
        int count = MaxCount;//getting 1K records 
        XmlDocument outdoc = new XmlDocument();
        var r = outdoc.CreateElement("e");

        while (!doc.EOF)
        {

            if (doc.Name == RecordTag)
            {
                //wrap Records into <l> tag in case we would need to transfer more infor within a Record.
                XmlElement rootNode = outdoc.CreateElement("l");
                rootNode.InnerXml = doc.ReadOuterXml();
                r.AppendChild(rootNode);

                count -= 1;
                if (count < 0)
                {
                    outdoc.AppendChild(r);
                    count = MaxCount;
                    ops.Add(CreateGlobalProductMsg(outdoc, RequestTime));
                    outdoc = new XmlDocument();
                    r = outdoc.CreateElement("e");
                }
            } else {
                doc.Read();
            }
        }

        if (count != MaxCount)
        {
            outdoc.AppendChild(r);
            ops.Add(CreateGlobalProductMsg(outdoc, RequestTime));
        }

        Debug.Write("MasterProductSplitter:parts created:" + this.Length);
        ins.Seek(0, SeekOrigin.End);
    }


    public bool hasData()
    {
        return Current < Length;
    }


    public void LogError(string message, Exception e)
    {
        LogError(message + "" + e.ToString());
    }
    public void LogError(string err)
    {
        Debug.Write("MasterProductSplitter:Error:" + err);
        EventLog.WriteEntry("Biztalk Server", "MasterProductSplitter:Error:" + err, System.Diagnostics.EventLogEntryType.Error, 1);
    }

    public XmlDocument GetPart()
    {
        XmlDocument xmldoc = new XmlDocument();
        try
        {
            StreamReader sr = new StreamReader(ops[Current]);
            xmldoc.Load(sr);
            Current += 1;
        }
        catch (Exception ex)
        {
            Current += 1;//this part is broken , moving to the next part
            LogError("MasterProductSplitter:Error reading part:", ex);
        }

        return xmldoc;
    }

    public static string CreateGlobalProductMsg(XmlDocument globalProductsMessage, DateTime RequestTime)
    {
        string ret = Path.GetTempFileName();
        using (FileStream res = new FileStream(ret, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite, 4096))
        {
            StreamWriter sb = new StreamWriter(res);
            sb.Write("<ns0:MasterProductSync xmlns:ns0=\"http://tempuri.org/\"><ns0:masterProducts><![CDATA[<r RequestTime=\"");
            sb.Write(RequestTime.ToString("s"));
            sb.Write("\">");
            sb.Write(globalProductsMessage.InnerXml);
            sb.Write("</r>]]></ns0:masterProducts></ns0:MasterProductSync>");
            res.Flush();
            sb.Flush();
            sb.Close();
            res.Close();
            Debug.Write("Create file:" + ret);
        }
        return ret;
    }



    /// <summary>
    ///  Copy data from one stream into anoter
    /// </summary>
    /// <param name="input"></param>
    /// <param name="output"></param>
    public void CopyStream(Stream input, Stream output)
    {
        byte[] buffer = new byte[8 * 1024];
        int len;
        while ((len = input.Read(buffer, 0, buffer.Length)) > 0)
        {
            output.Write(buffer, 0, len);
        }
    }



    /// <summary>
    /// Saving Stream Into File
    /// </summary>
    /// <param name="stm"></param>
    /// <param name="filename"></param>
    public static void SaveStreamIntoFile(Stream stm, string filename)
    {
        stm.Seek(0, SeekOrigin.Begin);
        FileStream fileStream = File.Create(filename, (int)stm.Length);
        byte[] bytesInStream = new byte[stm.Length];
        stm.Read(bytesInStream, 0, bytesInStream.Length);
        // Use write method to write to the file specified above
        fileStream.Write(bytesInStream, 0, bytesInStream.Length);
        fileStream.Close();
    }

}
   

No comments:

test smtp server with powershell

Send-MailMessage -SMTPServer smtp.domain.com -To [email protected] -From [email protected] -Subject "This is a test email" -Body ...