XML File:
<?xml version="1.0" encoding="utf-8"?>
<Data xmlns:xsd="http://www.hbjhgjgjg.com/1889/XMLSchema"
xmlns:xsi="http://www.hbjhgjgjg.com/1889/XMLSchema-instance"
xmlns="http://www.ytutu.com/data/schema/XXXa/1829">
<Header>
<ContentDate>2018-03-16T00:00:37.511-06:00</ContentDate>
<Originator>12344</Originator>
<FileContent>abc</FileContent>
<RecordCount>890</RecordCount>
</Header>
<Records>
<XXXRecord>
<XXX>8888834</XXX>
<Entity>
<Name xml:lang="es">NAME CV</Name>
<OtherEntityNames>
<OtherEntityName xml:lang="es" type="OTHER NAME"></OtherEntityName>
<OtherEntityName xml:lang="es" type="OTHER NAME1">ABGT</OtherEntityName>
</OtherEntityNames>
<Address xml:lang="es">
<FirstAddressLine>898 RTT</FirstAddressLine>
<AddressNumber>32</AddressNumber>
<AdditionalAddressLine>eddff</AdditionalAddressLine>
<AdditionalAddressLine>hghg</AdditionalAddressLine>
<City>CITY</City>
<Region>REGION</Region>
<Country>COUNTRY</Country>
<PostalCode>79799</PostalCode>
</Address>
<FirstAddress xml:lang="es">
<FirstAddressLine>rafgaf</FirstAddressLine>
<AddressNumber>32</AddressNumber>
<AdditionalAddressLine>gggg</AdditionalAddressLine>
<AdditionalAddressLine>jjgjgg</AdditionalAddressLine>
<City>CITY</City>
<Region>REGION</Region>
<Country>COUNTRY</Country>
<PostalCode>34433</PostalCode>
</FirstAddress>
<OtherAddresses>
<OtherAddress xml:lang="es" type="ADDRESS">
<FirstAddressLine>ggh</FirstAddressLine>
<AddressNumber>32</AddressNumber>
<AdditionalAddressLine>guhgu</AdditionalAddressLine>
<AdditionalAddressLine>tryryry</AdditionalAddressLine>
<City>CITY</City>
<Region>REGION</Region>
<Country>COUNTRY</Country>
<PostalCode>09090</PostalCode>
</OtherAddress>
</OtherAddresses>
<Confirmation>
<ID>77878</ID>
<EntityID>dsfdd</EntityID>
</Confirmation>
</Entity>
</Record>
and many similar records below...
I used the below code to convert XML to EXCEL file. I am using .net windows application. But the output excel file shows only the header data with 4 row counts. It is not reading all the values from the dataset. The dataset has all the values.
Is there any other method I can implement this?
private void Convert_Click(object sender, EventArgs e)
{
progressBar1.Value = 0;
if (checkBox1.Checked && txtCustomeFileName.Text != "" && txtXmlFilePath.Text != "") // using Custome Xml File Name
{
if (File.Exists(txtXmlFilePath.Text))
{
string CustXmlFilePath = Path.Combine(new FileInfo(txtXmlFilePath.Text).DirectoryName, txtCustomeFileName.Text); // Creating Path for Xml Files
System.Data.DataTable dt = CreateDataTableFromXml(txtXmlFilePath.Text);
ExportDataTableToExcel(dt, CustXmlFilePath);
MessageBox.Show("Conversion Completed!!");
}
}
else if (!checkBox1.Checked || txtXmlFilePath.Text != "") // Using Default Xml File Name
{
if (File.Exists(txtXmlFilePath.Text))
{
FileInfo fi = new FileInfo(txtXmlFilePath.Text);
string XlFile = fi.DirectoryName + "\\" + fi.Name.Replace(fi.Extension, ".xlsx");
System.Data.DataTable dt = CreateDataTableFromXml(txtXmlFilePath.Text);
ExportDataTableToExcel(dt, XlFile);
MessageBox.Show("Conversion Completed!!");
}
}
else
{
MessageBox.Show("Please Fill Required Feilds!!");
}
}
// Creating DataTable With Xml Data
public System.Data.DataTable CreateDataTableFromXml(string XmlFile)
{
System.Data.DataTable Dt = new System.Data.DataTable();
try
{
DataSet ds = new DataSet();
ds.ReadXml(XmlFile);
Dt.Load(ds.CreateDataReader());
}
catch (Exception ex)
{
}
return Dt;
}
private void ExportDataTableToExcel(System.Data.DataTable table, string Xlfile)
{
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
Workbook book = excel.Application.Workbooks.Add(Type.Missing);
excel.Visible = false;
excel.DisplayAlerts = false;
Worksheet excelWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)book.ActiveSheet;
excelWorkSheet.Name = table.TableName;
progressBar1.Maximum = table.Columns.Count;
for (int i = 1; i < table.Columns.Count + 1; i++) // Creating Header Column In Excel
{
excelWorkSheet.Cells[1, i] = table.Columns[i - 1].ColumnName;
if (progressBar1.Value < progressBar1.Maximum)
{
progressBar1.Value++;
int percent = (int)(((double)progressBar1.Value / (double)progressBar1.Maximum) * 100);
progressBar1.CreateGraphics().DrawString(percent.ToString() + "%", new System.Drawing.Font("Arial", (float)8.25, FontStyle.Regular), Brushes.Black, new PointF(progressBar1.Width / 2 - 10, progressBar1.Height / 2 - 7));
System.Windows.Forms.Application.DoEvents();
}
}
progressBar1.Maximum = table.Rows.Count;
for (int j = 0; j < table.Rows.Count; j++) // Exporting Rows in Excel
{
for (int k = 0; k < table.Columns.Count; k++)
{
excelWorkSheet.Cells[j + 2, k + 1] = table.Rows[j].ItemArray[k].ToString();
}
if (progressBar1.Value < progressBar1.Maximum)
{
progressBar1.Value++;
int percent = (int)(((double)progressBar1.Value / (double)progressBar1.Maximum) * 100);
progressBar1.CreateGraphics().DrawString(percent.ToString() + "%", new System.Drawing.Font("Arial", (float)8.25, FontStyle.Regular), Brushes.Black, new PointF(progressBar1.Width / 2 - 10, progressBar1.Height / 2 - 7));
System.Windows.Forms.Application.DoEvents();
}
}
book.SaveAs(Xlfile);
book.Close(true);
excel.Quit();
Marshal.ReleaseComObject(book);
Marshal.ReleaseComObject(book);
Marshal.ReleaseComObject(excel);
}
}
Is there any other method I can use?