how fill datatable from xml file in c# windows forms?

Answered how fill datatable from xml file in c# windows forms?

  • Tuesday, March 13, 2012 1:39 PM
     
     

    Hi friends

    I want to fill the datatable from xml file.

    this is my code, but it is not working. when i bind the datatable into gridview there is no rows.

    DataSet ds = new DataSet(); 

    ds.ReadXml("XMLEmployee.xml");

    dataGridView1.DataSource = ds.Tables[0].DefaultView;

    please help me to do this.

    thanks in advance

    by

    faisal


    faisal

    • Moved by CoolDadTxMVP Tuesday, March 13, 2012 2:38 PM Winforms related (From:Visual C# General)
    •  

All Replies

  • Tuesday, March 13, 2012 3:06 PM
     
     Answered Has Code

    Hi faisal;

    If the XML document contains a schema in it then you code should have worked.

    DataSet ds = new DataSet();
    ds.ReadXml("XMLEmployee.xml");
    dataGridView1.DataSource = ds.Tables[0].DefaultView;

    So I am assuming that the XML file does not contain a schema in the document. In that case the next best thing is to create a DataTable object with all the correct column names and data type that are contain in the XML file. For example lest say I have this XML Document:

    <?xml version="1.0" standalone="yes"?>
    <DocumentElement>
      <Northwind>
        <ProductID>1</ProductID>
        <ProductName>Chai</ProductName>
        <UnitPrice>18.0000</UnitPrice>
        <UnitsInStock>39</UnitsInStock>
        <UnitsOnOrder>0</UnitsOnOrder>
      </Northwind>
      <Northwind>
        <ProductID>2</ProductID>
        <ProductName>Chang</ProductName>
        <UnitPrice>19.0000</UnitPrice>
        <UnitsInStock>17</UnitsInStock>
        <UnitsOnOrder>40</UnitsOnOrder>
      </Northwind>
      <Northwind>
        <ProductID>3</ProductID>
        <ProductName>Aniseed Syrup</ProductName>
        <UnitPrice>10.0000</UnitPrice>
        <UnitsInStock>13</UnitsInStock>
        <UnitsOnOrder>70</UnitsOnOrder>
      </Northwind>
    <DocumentElement>

    then I would need to create the table as follows:

    DataTable dt = new DataTable("Northwind");
    DataColumn dc = new DataColumn("ProductID", typeof(int));
    dt.Columns.Add(dc);
    dc = new DataColumn("ProductName", typeof(string));
    dt.Columns.Add(dc);
    dc = new DataColumn("UnitPrice", typeof(decimal));
    dt.Columns.Add(dc);           
    dc = new DataColumn("UnitsInStock", typeof(short));
    dt.Columns.Add(dc);           
    dc = new DataColumn("UnitsOnOrder", typeof(short));
    dt.Columns.Add(dc);

    then I can do this :

    dt.ReadXml("C:/Working Directory/XMLFileName.xml");

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

  • Tuesday, March 13, 2012 4:47 PM
     
     

    Hi fernanto

    thanks for your reply....

    i have changed the path. now my code is working

    thank you.


    faisal

  • Wednesday, June 13, 2012 7:12 PM
     
     

    Fernando, how would I do this if I wanted to use a node's attribute as one of the DT column values, and if the XML was a string, not a file?  For example my XML is:

    <?xml version="1.0" encoding="utf-8"?><blahs><blah id="9">foo</blah><blah id="24">bar</blah></blahs>

    And i want to populate my DT with 2 columns such that it contains

    Col1 Col2

    9 foo

    24 bar


    • Edited by double e Wednesday, June 13, 2012 7:21 PM
    •  
  • Wednesday, June 13, 2012 7:57 PM
     
     Answered Has Code

    Hello double e;

    The following code should do what you need. Also please new questions need to be ask in a new thread, Thank you.

    // Create a data table with the correct columns and data type for the columns
    // as shown here or use an existing table if one exist.
    DataTable dt = new DataTable("MyBlahsDataTable");
    DataColumn dc = new DataColumn("Col1", typeof(int));
    dt.Columns.Add(dc);
    dc = new DataColumn("Col2", typeof(string));
    dt.Columns.Add(dc);
    
    // This is the XML in a string format.
    string xmlStr = "<?xml version=\"1.0\" encoding=\"utf-8\"?><blahs>	<blah id=\"9\">foo</blah>	<blah id=\"24\">bar</blah></blahs>";
    
    // This loads the XML string into an XML document
    XDocument xdoc = XDocument.Parse(xmlStr);
    
    // Get all the blah nodes and extract there values
    var fields = from f in xdoc.Root.Descendants("blah")
                 select new {
                    col1 = (int) f.Attribute("id"),
                    col2 = (string) f.Value
                 };
    
    // Load the data table with the values             
    foreach (var column in fields)
    {
        DataRow dr = dt.NewRow();
        dr["Col1"] = column.col1;
        dr["Col2"] = column.col2;
        dt.Rows.Add(dr);
    } 

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".