2013-01-24

Parsing XML files to Excel or webpage

I found myself needed to go over a large number of XML files, to check the content. I love XML, it's simple structure and flexibility but you do not want to use XML to browse through thousands of rows of data, the structure simple does not allow that. So what do you do? I wrote a very simple program that loops over all files in a directory, opens the files and outputs the key data in a table format, either to Excel or to a web page. The result is easy to read, easy to summarize and analyze.

Write this in a windows form, web form or even an CMD application.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.IO;
using System.Drawing;
using Microsoft.Office.Interop.Excel;

namespace ParseXMLFiles
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ParseXML();
            ParseXMLToExcel();
        }

        public void ParseXMLToExcel()
        {  
            int NoOfFiles = 1;
            int NoOfNodes = 1;
            string folder = "\\\\euex1app11\\data\\BaseLogistics\\SOPackingSlip\\Original\\";

            string[] files = Directory.GetFiles(folder, "*.xml");

            Microsoft.Office.Interop.Excel.Application app = new Application();        
Microsoft.Office.Interop.Excel.Workbook workbook = app.Workbooks.Add(System.Reflection.Missing.Value);
Microsoft.Office.Interop.Excel.Worksheet sheet = workbook.Sheets.Add(System.Reflection.Missing.Value);

            sheet.Cells[1, 1] = "Rec ID";
            sheet.Cells[1, 2] = "File";
            sheet.Cells[1, 3] = "Item ID";
            sheet.Cells[1, 4] = "Sales ID";
            sheet.Cells[1, 5] = "Quantity";
            NoOfNodes++;

            foreach (string file in files)
            {

                XmlDocument doc = new XmlDocument();
                doc.Load(file);

                XmlNode root = doc.DocumentElement;

                XmlNodeList node = root.SelectNodes("SalesTable/SalesLine/Fields");

                foreach (XmlNode nodes in node)
                {                
                    sheet.Cells[NoOfNodes, 1] = nodes["RecId"].InnerText;                
                    sheet.Cells[NoOfNodes, 2] = file;                
                    sheet.Cells[NoOfNodes, 3] = nodes["ItemId"].InnerText;                
                    sheet.Cells[NoOfNodes, 4] = nodes["SalesId"].InnerText;                
                    sheet.Cells[NoOfNodes, 5] = nodes["InventDeliverNow"].InnerText;
                    NoOfNodes++;
                }
                NoOfFiles++;
            }
            app.Visible = true;
        }    
               
        public void ParseXML()
        {
            TableRow row;
            TableCell cell;
            TableCell cell1;
            TableCell cell2;
            TableCell cell3;
            TableCell cell4;

            int NoOfFiles = 1;
            int NoOfNodes = 1;
            string folder = "\\\\euex1app11\\data\\BaseLogistics\\SOPackingSlip\\Original\\";

            string[] files = Directory.GetFiles(folder, "*.xml");
                       
            NoOfNodes++;
            foreach (string file in files)
            {

                XmlDocument doc = new XmlDocument();
                doc.Load(file);

                XmlNode root = doc.DocumentElement;

                XmlNodeList node = root.SelectNodes("SalesTable/SalesLine/Fields");
               
                foreach (XmlNode nodes in node)
                {
                    row = new TableRow();
                    cell = new TableCell();
                    cell1 = new TableCell();
                    cell2 = new TableCell();
                    cell3 = new TableCell();
                    cell4 = new TableCell();

                    cell.Text = nodes["RecId"].InnerText;                   
                    cell1.Text = file;                   
                    cell2.Text = nodes["ItemId"].InnerText;                   
                    cell3.Text = nodes["SalesId"].InnerText;                   
                    cell4.Text = nodes["InventDeliverNow"].InnerText;                   
                    row.Cells.Add(cell);
                    row.Cells.Add(cell1);
                    row.Cells.Add(cell2);
                    row.Cells.Add(cell3);
                    row.Cells.Add(cell4);
                    Table1.Rows.Add(row);
                    NoOfNodes++;
                }
                NoOfFiles++;
            }
           
        }      

    }
}

Inga kommentarer:

Skicka en kommentar