locked
Xml RRS feed

  • Question

  • Hi,

    How to bind xml data into a data table using c# code?

    Can u send code?

    plz...........

    Wednesday, March 7, 2012 11:00 AM

Answers

  • Using Linq you can achieve this in the following way

    public DataTable XElementToDataTable(XElement x)

      DataTable dt = new DataTable();

      XElement setup = (from p in x.Descendants() select p).First();
      foreach (XElement xe in setup.Descendants()) // build your DataTable
        dt.Columns.Add(new DataColumn(xe.Name.ToString(),typeof(string))); // add columns to your dt

      var all = from p in x.Descendants(setup.Name.ToString()) select p;
      foreach (XElement xe in all)
      {
        DataRow dr = dt.NewRow();
        foreach (XElement xe2 in xe.Descendants())
          dr[xe2.Name.ToString()] = xe2.Value; //add in the values
        dt.Rows.Add(dr);
      }
      return dt;
    }

    Wednesday, March 7, 2012 1:08 PM
  • There are (2) options below: 1 for reading raw XML, and another from reading the XML from a file. They should accomplish what you need

    //Option 1: Read the raw XML into a StringReader and then load into a DataSet, which then you can get the DataTable
    string XMLContent = "<tag1>Blah</tag1><tag2>Blah Blah</tag2>";
    System.IO.StringReader sr = new System.IO.StringReader(XMLContent);
    DataSet ds1 = new DataSet();
    ds1.ReadXml(sr);
    //Get the DataTable object from the dataset that contains the XML:
    DataTable dt1 = ds1.Tables[0];

    //Option 2: Load a XML file directly into a DataSet, which then you can get the DataTable
    string XMLFile = "C:\\MyFiles\\MyXMLFile.xml";
    DataSet ds2 = new DataSet();
    ds2.ReadXml(XMLFile);
    //Get the DataTable object from the dataset that contains the XML: 
    DataTable dt2 = ds1.Tables[0];

    Hope this helps! :)


    Thank you,



    • Edited by atconway Wednesday, March 7, 2012 4:05 PM
    • Proposed as answer by atconway Thursday, March 8, 2012 7:47 PM
    • Marked as answer by Lie YouModerator Friday, March 9, 2012 3:24 AM
    Wednesday, March 7, 2012 4:01 PM

All replies

  • Using Linq you can achieve this in the following way

    public DataTable XElementToDataTable(XElement x)

      DataTable dt = new DataTable();

      XElement setup = (from p in x.Descendants() select p).First();
      foreach (XElement xe in setup.Descendants()) // build your DataTable
        dt.Columns.Add(new DataColumn(xe.Name.ToString(),typeof(string))); // add columns to your dt

      var all = from p in x.Descendants(setup.Name.ToString()) select p;
      foreach (XElement xe in all)
      {
        DataRow dr = dt.NewRow();
        foreach (XElement xe2 in xe.Descendants())
          dr[xe2.Name.ToString()] = xe2.Value; //add in the values
        dt.Rows.Add(dr);
      }
      return dt;
    }

    Wednesday, March 7, 2012 1:08 PM
  • There are (2) options below: 1 for reading raw XML, and another from reading the XML from a file. They should accomplish what you need

    //Option 1: Read the raw XML into a StringReader and then load into a DataSet, which then you can get the DataTable
    string XMLContent = "<tag1>Blah</tag1><tag2>Blah Blah</tag2>";
    System.IO.StringReader sr = new System.IO.StringReader(XMLContent);
    DataSet ds1 = new DataSet();
    ds1.ReadXml(sr);
    //Get the DataTable object from the dataset that contains the XML:
    DataTable dt1 = ds1.Tables[0];

    //Option 2: Load a XML file directly into a DataSet, which then you can get the DataTable
    string XMLFile = "C:\\MyFiles\\MyXMLFile.xml";
    DataSet ds2 = new DataSet();
    ds2.ReadXml(XMLFile);
    //Get the DataTable object from the dataset that contains the XML: 
    DataTable dt2 = ds1.Tables[0];

    Hope this helps! :)


    Thank you,



    • Edited by atconway Wednesday, March 7, 2012 4:05 PM
    • Proposed as answer by atconway Thursday, March 8, 2012 7:47 PM
    • Marked as answer by Lie YouModerator Friday, March 9, 2012 3:24 AM
    Wednesday, March 7, 2012 4:01 PM
  • Hi,

    got it...........

    thanx ur reply..........

    Thursday, March 8, 2012 11:35 AM
  • Please mark which post helped you and was the answer providing quick and easy solution determination for future readers. Thank you.

    Thank you,

    Thursday, March 8, 2012 7:49 PM