Read data from IRestResponse to DataTable using C# and VB.Net in ASP.Net

Chriz
 
on Aug 27, 2021 04:07 AM
Sample_433018.zip
693 Views

Hello,

I get the following response.Content from a REST Request. 

<GetEmployeeResponse xmlns="http://mytest.dev.com/">
    <PropertyStatus xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
        <Employee>
            <Name>John</Name>
            <Id>6584</Id>
        </Employee>
        <Employee>
            <Name>Chris</Name>
            <Id>4120</Id>
        </Employee>
    </PropertyStatus>
</GetEmployeeResponse>

How can I append the rows of the above Employees to a datatable?

I used this example Read XML File and bind it to GridView in ASP.Net but I get a column named PropertyStatus_id = 0 instead of the employees.

Here's my code. 

IRestResponse response= reqEmployee(app);
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(response.Content);
XmlReader xmlReader = new XmlNodeReader(xmlDoc);
ds_response.ReadXml(xmlReader);
GridView1.DataSource = ds_response;
GridView1.DataBind();

Thank you in advance.

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Aug 27, 2021 07:18 AM

Hi Chriz,

Refer below code.

HTML

<asp:GridView runat="server" ID="GridView1" AutoGenerateColumns="false">
    <Columns>
        <asp:BoundField DataField="Id" HeaderText="Id" />
        <asp:BoundField DataField="Name" HeaderText="Name" />
    </Columns>
</asp:GridView>

Namespaces

C#

using System.Data;
using System.Xml;

VB.Net

Imports System.Data
Imports System.Xml

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    DataSet ds_response = new DataSet();
    string xml = System.IO.File.ReadAllText(Server.MapPath("~/XML.txt"));
    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.LoadXml(xml);
    XmlReader xmlReader = new XmlNodeReader(xmlDoc);
    ds_response.ReadXml(xmlReader);
    GridView1.DataSource = ds_response.Tables["Employee"];
    GridView1.DataBind();
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    Dim ds_response As DataSet = New DataSet()
    Dim xml As String = System.IO.File.ReadAllText(Server.MapPath("~/XML.txt"))
    Dim xmlDoc As XmlDocument = New XmlDocument()
    xmlDoc.LoadXml(xml)
    Dim xmlReader As XmlReader = New XmlNodeReader(xmlDoc)
    ds_response.ReadXml(xmlReader)
    GridView1.DataSource = ds_response.Tables("Employee")
    GridView1.DataBind()
End Sub