How to get nested XML from tabbed Excel Spreadsheet using C# on .NET

umunmikee

New Member
Nested XML from Tabbed Excel Spreadsheet C# .NETI am trying to format the data from multiple sheets in an excel spreadshet as nested xmlThe xml I would like to end up with is \[code\]<root><Tab> <TabName>Tab 0</TabName> <Products> <ItemId>11111</ItemId> <Image>/images/100.jpg</Image> <Link>http://www.google.com</Link> </Products> <Products> <ItemId>22222</ItemId> <Image>/images/100.jpg</Image> <Link>http://www.google.com</Link> </Products></Tab><Tab> <TabName>Tab 1</TabName> <Products> <ItemId>11111</ItemId> <Image>/images/100.jpg</Image> <Link>http://www.google.com</Link> </Products> <Products> <ItemId>22222</ItemId> <Image>/images/100.jpg</Image> <Link>http://www.google.com</Link> </Products></Tab></root>\[/code\]The code i am using is \[code\]public static string Ajax_ReadExcel(){ OleDbConnection objConn = null; System.Data.DataTable dt = null; string fileName = HttpRuntime.AppDomainAppPath + "ExcelUpload\\ProductData.xlsx"; DataSet objDataset1 = new DataSet("root"); try { // Connection String. Change the excel file to the file you // will search. String connString = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + fileName + ";Extended Properties=Excel 8.0;"; connString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0}; Extended Properties=Excel 12.0;", fileName); // Create connection object by using the preceding connection string. objConn = new OleDbConnection(connString); // Open connection with the database. objConn.Open(); // Get the data table containg the schema guid. dt = objConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null); //dt = objConn.E if (dt == null) { return null; } String[] excelSheets = new String[dt.Rows.Count]; int i = 0; // Add the sheet name to the string array. foreach (DataRow row in dt.Rows) { excelSheets = row["TABLE_NAME"].ToString(); i++; } // Loop through all of the sheets if you want too... for (int j = 0; j < excelSheets.Length; j++) { OleDbCommand objCmdSelect = new OleDbCommand("SELECT * FROM ["+excelSheets[j]+"]", objConn); OleDbDataAdapter objAdapter1 = new OleDbDataAdapter(); objAdapter1.SelectCommand = objCmdSelect; objAdapter1.Fill(objDataset1, "Products"); // Query each excel sheet. //Trace.Write(objDataset1.GetXml()); } //ltrContent.Text += "<hr/>" + excelSheets; objConn.Close(); //return excelSheets; } catch (Exception ex) { return null; } finally { // Clean up. if (objConn != null) { objConn.Close(); objConn.Dispose(); } if (dt != null) { dt.Dispose(); } } return objDataset1.GetXml();}\[/code\]I can load all of the excel data into one xml set\[code\]<root><Products>...</Products></root>\[/code\], but I am not familiar enough with the C# DataSet to have the output transformed into the nested xml I need.
 
Back
Top