Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dot is implicitly converted into hash while converting data from Excel file to XML

Tags:

c#

xml

excel

oledb

I have succeeded in creating XML file from an Excel file using the following C# code:

protected void Button5_Click(object sender, EventArgs e)
{
    if (FileUpload1.HasFile)
    {
        OleDbConnection ole = new OleDbConnection();

        string s = Server.MapPath("../admin/ProductOptions");
        s = s + "\\" + FileUpload1.FileName;
        System.IO.File.Delete(s);
        FileUpload1.PostedFile.SaveAs(s);

        string path = s;
        ole.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + path + ";" + "Extended Properties=" + "\"" + "Excel 12.0;HDR=YES;" + "\"";
        OleDbCommand command = new OleDbCommand("select * from[SHEET1$]", ole);
        DataSet ds = new DataSet();
        OleDbDataAdapter adapter = new OleDbDataAdapter(command);
        adapter.Fill(ds);

        GridView1.DataSource = ds.Tables[0];
        GridView1.DataBind();
        GridView1.Visible = true;

       string filepath = Server.MapPath("ProductOptions") + "\\" + DDLproduct.SelectedValue + ".xml";
        Session["ss"] = ds;

        write_to_xml(ds,filepath);
    }
    else
    {
        Label2.Visible = true;
        Label2.Text="[Please Select a file]";
    }
}

But the problem is when this code is converting the Excel Data to XML data, then dots are itself converted into Hash(Only First Row). I know the reason but don't know the solution.
It`s happening because of dots in Excel file when converted into XML tags them implicitly converted to HASH.......
Kindly suggest me, how can I stop this conversion?

like image 222
user1206552 Avatar asked Oct 09 '22 16:10

user1206552


1 Answers

Finally got the solution:

  1. When OLEDB Adapter fills the data in DataSet, it converts DOT into HASH.

  2. Now I have stored that data into a DataTable(dt) and then accessed the column name and replace HASH with DOT (using Replace method of String) and create a new DataTable(dt2) with new column names.

  3. After this using two for loops, I have inserted data from first DataTable(dt) to new Datatable(dt2). (*one loop for rows and another one for columns)

  4. Finally bind the grid with new DataTable(dt2)

Following is the full code for that function:

if (FileUpload1.HasFile)
{
    OleDbConnection ole = new OleDbConnection();

    string s = Server.MapPath("../admin/ProductOptions");
    s = s + "\\" + FileUpload1.FileName;

    FileUpload1.PostedFile.SaveAs(s);
    string path = s;

    ole.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + path + ";" + "Extended Properties=" + "\"" + "Excel 12.0;HDR=YES;IMEX=2;READONLY=FALSE;" + "\" ";

    OleDbCommand command = new OleDbCommand("select * from[SHEET1$]", ole);

    DataSet ds = new DataSet();
    OleDbDataAdapter adapter = new OleDbDataAdapter(command);
    adapter.Fill(ds);

    DataTable dt = (DataTable)ds.Tables[0];
    DataTable dt2 = new DataTable("dt2");
    Session["dt"] = null;

    for (int i = 0; i < dt.Columns.Count; i++)
    {
        string s2 = dt.Columns[i].ToString();
        s2 = s2.Replace("#", ".");

        string ProductName = s2.ToString();
        if (Session["dt"] == null)
       {
            DataColumn dCol1 = new DataColumn(ProductName, typeof(System.String));
           dt2.Columns.Add(dCol1);
        }
    }

    for (int i = 0; i < dt.Rows.Count; i++)
    {
        dt2.Rows.Add();
        for (int x = 0; x < dt.Columns.Count; x++)
        {                
            dt2.Rows[i][x] = dt.Rows[i][x];        
        }        
    }

    System.IO.File.Delete(s);

    GridView1.DataSource = dt2;
    GridView1.DataBind();        
    GridView1.Visible = true;

    string filepath = Server.MapPath("ProductOptions") + "\\" + DDLproduct.SelectedValue + ".xml";
   // Session["ss"] = ds;

    write_to_xml(dt2,filepath);
}
else
{
    Label2.Visible = true;
    Label2.Text="[Please Select a file]";
}

Following is the code for write_to_xml() :

public void write_to_xml(DataTable dt, string path)
{
    dt.WriteXml(path);
}

Any query or alternative solution would be appreciated... :)

like image 130
user1206552 Avatar answered Oct 12 '22 09:10

user1206552