Convert XML to DataTable

The article explains how to convert xml to datatable. A xml file is read from the hard coded url. The xml content is downloaded as a string and processed using string reader.

The data read from the xml sting are stored in datatable and displayed as output. Use String Reader to load xml string into a DataSet. The table with first index has the converted string.

The current DataTable and its descendants are loaded with the data from the url named in the supplied string.

The ReadXml method provides a way to read either data only, or both data and schema into a DataTable from an XML document, whereas the ReadXmlSchema method reads only the schema.

Note that the same is true for the WriteXml and WriteXmlSchema methods, respectively. To write XML data, or both schema and data from the DataTable, use the WriteXml method. To write just the schema, use the WriteXmlSchema method.


Add below code in Default.aspx page

    <form id="form1" runat="server">
        <h3>Convert XML to DataTable</h3>
            <br />
            <br />
            <asp:Button ID="btn_Convert" runat="server" Text="Convert XML to DataTable" OnClick="btn_Convert_Click" />
            <br />
            <br />
            <br />
            <asp:GridView ID="gv_Display" runat="server"></asp:GridView>


protected void btn_Convert_Click(object sender, EventArgs e)
          string InputURL = string.Empty;
          WebClient web = new WebClient();
          InputURL = string.Format(",%20share_count,%20like_count,%20comment_count,%20total_count,%20click_count%20FROM%20link_stat%20where%20url=");
          string response = web.DownloadString(InputURL);
          DataSet ds = new DataSet();
          using (StringReader stringReader = new StringReader(response))
              ds = new DataSet();
          DataTable dt = ds.Tables[0];

          gv_Display.DataSource = dt;


Output - Convert xml to datatable

Demo Code to Convert XML to DataTable

Download Demo