How to parse hierarchial XML from Excel sheet

278 Views Asked by At

I am trying to parse an XML file as provided below with C# and XmlTextWriter

Example:

<ChessBoard>
<King>
    <Bishop>
        <Soldier/>
    </Bishop>
</King>
<Queen>
    <Tower/>
</Queen>
</ChessBoard>

from an Excel workbook, that contains one slide for each element and on the first column is a list of its child elements. Link to example Excel workbook if (when) the explanation did not make itself clear :) : https://www.yousendit.com/download/TEhYc0x3NDQrV3h1a3NUQw

I don't know how many sheets the user will make or the number of elements/child items beforehand, so the parsing would need to be dynamical.

If someone could point me in the right direction that would be greatly appreciated. If needed I can provide the (non-working) code I have so far if someone wants to use it as a starting point.

Thanks in advance!

EDIT-- code so far

 Excel.Application xlApp;
        xlApp = new Excel.Application();
        Excel.Workbook xlWorkBook = null;
        Excel.Worksheet xlWorkSheet;
        Excel.Range range;
        try
        {

            string str = string.Empty;
            xlWorkBook = xlApp.Workbooks.Open(Excel_location + @"\Tree.xls",
             Type.Missing, Type.Missing, Type.Missing, Type.Missing,
             Type.Missing, Type.Missing, Type.Missing, Type.Missing,
             Type.Missing, Type.Missing, Type.Missing, Type.Missing,
             Type.Missing, Type.Missing);


            using (XmlTextWriter writer = new XmlTextWriter(@"Tree.xml", Encoding.UTF8))
            {

                int rCnt = 0;// rCnt = row count
                writer.WriteStartDocument();


                Excel.Sheets sheets = xlWorkBook.Worksheets;
                Excel.Range sheetrange = xlWorkSheet.UsedRange;

                List<Excel.Worksheet> Sheets = new List<Excel.Worksheet>();
                foreach (Excel.Worksheet s in xlWorkBook.Worksheets)
                {
                    Sheets.Add(s);
                }


                 bool first = true;
                 foreach (Excel.Worksheet s in Sheets)
                {
                    if (first)
                    {
                        first = false;
                    sheetrange = s.UsedRange;
                    writer.WriteStartElement(s.Name);
                    for (rCnt = 1; rCnt <= sheetrange.Rows.Count; rCnt++)
                    {
                        string root = (string)(sheetrange.Cells[rCnt, 1] as Excel.Range).Value2.ToString();
                        writer.WriteStartElement(root);
                        foreach (Excel.Worksheet childsheet in Sheets)
                        {
                            if (childsheet.Name == root)
                            {

                                Excel.Range childsheetrange = childsheet.UsedRange;
                                for (int crCnt = 1; crCnt <= childsheetrange.Rows.Count; crCnt++)
                                {
                                    str = (string)(childsheetrange.Cells[crCnt, 1] as Excel.Range).Value2.ToString();
                                    writer.WriteStartElement(str);
                                    foreach (Excel.Worksheet child2sheet in Sheets)
                                    {
                                        if (child2sheet.Name == str)
                                        {
                                            Excel.Range child2sheetrange = child2sheet.UsedRange;
                                            for (int ccrCnt = 1; ccrCnt <= child2sheetrange.Rows.Count; ccrCnt++)
                                            {
                                                string str2 = (string)(child2sheetrange.Cells[ccrCnt, 1] as Excel.Range).Value2.ToString();
                                                writer.WriteStartElement(str2);
                                                writer.WriteEndElement();
                                            }
                                        }
                                    }


                                    writer.WriteEndElement();
                                }


                            }
                        }
                        writer.WriteEndElement();
                    }
                    writer.WriteEndElement();
                }
                }


            }
            xlWorkBook.Close(false, Excel_location + @"\Tree.xls", null);
            xlApp.Quit();
        }


        catch (Exception)
        {
            xlWorkBook.Close(false, Excel_location + @"\Tree.xls", null);
            xlApp.Quit();
            throw;
        }

Well obviously the code above poses a "minor" problem; when there are more than 2 child items the thing wont work :D Of course I could add 25 foreach loops but I would like to have a bit more sophisticated solution.. So, how to change the code so that it would work with arbitrary number of child elements?

0

There are 0 best solutions below