locked
Read xml and bind it in nested grid view RRS feed

  • Question

  • User-148788041 posted

    Hi,

    I have a xml like . I want to bind it in nested grid view , My nested grid is like this.

    <GridView id="gv" runnat="server">
    
    <Column>
    <BoundFile dataname="Profileid" HeaderText="Profile">
    
    
    <TemplateField>
    <BoundFile dataname="response" HeaderText="Response">
    <BoundFile dataname="error" HeaderText="Detail message">
    
    </TemplateField>
    </Column
    </Gridview>

    Errors.xml

    <ApiResponse>
    <Errors>
    <ProfileId>grghhfh</ProfileId>
    <message>Authentication </message>
    <response>403</response>
    <detail>
    <Detail>
    <message>Authentication </message>
    <response>403</response>
    
    </Detail>
    </detail>
    
    </Errors>
    
    </ApiResponse>
    Wednesday, November 29, 2017 1:30 AM

Answers

  • User-707554951 posted

    Hi guhananth,

    Firstly, your GridView is in wrong format.

    You coudl learn basic knowledge about gridview by refereing to the articals below:

    https://msdn.microsoft.com/en-us/library/2s019wc0(v=vs.100).aspx

    Besides, following  working sample for your reference:

    xml file:

    <?xml version="1.0" encoding="utf-8" ?>
    <ApiResponse>
      <Errors>
        <ProfileId>grghhfh</ProfileId>
        <message>Authentication </message>
        <response>403</response>
        <detail>
          <Detail>
            <message>Authentication</message>
            <response>403</response>
          </Detail>
        </detail>
      </Errors>
    </ApiResponse>
       <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" 
                    OnRowDataBound="GridView1_RowDataBound" DataKeyName="ProfileId">
                    <Columns>
                        <asp:BoundField DataField="ProfileId" HeaderText="ProfileId" />
                        <asp:BoundField DataField="message" HeaderText="message" />
                        <asp:BoundField DataField="response" HeaderText="response" />
                        <asp:TemplateField HeaderText="Detail">
                            <ItemTemplate>
                                <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="false">
                                    <Columns>
                                        <asp:BoundField DataField="message" HeaderText="message" />
                                        <asp:BoundField DataField="response" HeaderText="response" />
    
                                    </Columns>
                                </asp:GridView>
                            </ItemTemplate>
                        </asp:TemplateField>
    
                    </Columns>
                </asp:GridView>
    

    CodeBehind:

    using System.Web.UI.WebControls;
    using System.Xml;
    using System.Data;
    using System.Xml;

    XmlDocument doc2; protected void Page_Load(object sender, EventArgs e) { doc2 = new XmlDocument(); doc2.Load(Server.MapPath("Error.xml")); DataTable dt = new DataTable(); dt.Columns.AddRange(new DataColumn[] { new DataColumn("ProfileId"), new DataColumn("message"), new DataColumn("response"), }); XmlElement root1 = doc2.DocumentElement; XmlNodeList nodes1 = root1.SelectNodes("Errors"); foreach (XmlNode node in nodes1) { dt.Rows.Add(node.SelectNodes("ProfileId")[0].InnerText, node.SelectNodes("message")[0].InnerText, node.SelectNodes("response")[0].InnerText); } GridView1.DataSource = dt; ; GridView1.DataBind(); } protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { GridView gv2 = (GridView)e.Row.Cells[3].FindControl("GridView2"); string ProfileId = e.Row.Cells[0].Text.ToString(); XmlElement root = doc2.DocumentElement; XmlNodeList nodes = root.SelectNodes("Errors"); foreach (XmlNode node in nodes) { if(node.SelectNodes("ProfileId")[0].InnerText== ProfileId) { DataTable dt1 = new DataTable(); dt1.Columns.AddRange(new DataColumn[] { new DataColumn("message"), new DataColumn("response")}); XmlNodeList nodelist2 = node.SelectNodes("detail")[0].SelectNodes("Detail")[0].ChildNodes; dt1.Rows.Add(nodelist2[0].InnerText, nodelist2[1].InnerText); gv2.DataSource = dt1; gv2.DataBind(); } } } }

    Output:

    Best regards

    Cathy

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 29, 2017 3:23 AM

All replies

  • User-707554951 posted

    Hi guhananth,

    Firstly, your GridView is in wrong format.

    You coudl learn basic knowledge about gridview by refereing to the articals below:

    https://msdn.microsoft.com/en-us/library/2s019wc0(v=vs.100).aspx

    Besides, following  working sample for your reference:

    xml file:

    <?xml version="1.0" encoding="utf-8" ?>
    <ApiResponse>
      <Errors>
        <ProfileId>grghhfh</ProfileId>
        <message>Authentication </message>
        <response>403</response>
        <detail>
          <Detail>
            <message>Authentication</message>
            <response>403</response>
          </Detail>
        </detail>
      </Errors>
    </ApiResponse>
       <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" 
                    OnRowDataBound="GridView1_RowDataBound" DataKeyName="ProfileId">
                    <Columns>
                        <asp:BoundField DataField="ProfileId" HeaderText="ProfileId" />
                        <asp:BoundField DataField="message" HeaderText="message" />
                        <asp:BoundField DataField="response" HeaderText="response" />
                        <asp:TemplateField HeaderText="Detail">
                            <ItemTemplate>
                                <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="false">
                                    <Columns>
                                        <asp:BoundField DataField="message" HeaderText="message" />
                                        <asp:BoundField DataField="response" HeaderText="response" />
    
                                    </Columns>
                                </asp:GridView>
                            </ItemTemplate>
                        </asp:TemplateField>
    
                    </Columns>
                </asp:GridView>
    

    CodeBehind:

    using System.Web.UI.WebControls;
    using System.Xml;
    using System.Data;
    using System.Xml;

    XmlDocument doc2; protected void Page_Load(object sender, EventArgs e) { doc2 = new XmlDocument(); doc2.Load(Server.MapPath("Error.xml")); DataTable dt = new DataTable(); dt.Columns.AddRange(new DataColumn[] { new DataColumn("ProfileId"), new DataColumn("message"), new DataColumn("response"), }); XmlElement root1 = doc2.DocumentElement; XmlNodeList nodes1 = root1.SelectNodes("Errors"); foreach (XmlNode node in nodes1) { dt.Rows.Add(node.SelectNodes("ProfileId")[0].InnerText, node.SelectNodes("message")[0].InnerText, node.SelectNodes("response")[0].InnerText); } GridView1.DataSource = dt; ; GridView1.DataBind(); } protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { GridView gv2 = (GridView)e.Row.Cells[3].FindControl("GridView2"); string ProfileId = e.Row.Cells[0].Text.ToString(); XmlElement root = doc2.DocumentElement; XmlNodeList nodes = root.SelectNodes("Errors"); foreach (XmlNode node in nodes) { if(node.SelectNodes("ProfileId")[0].InnerText== ProfileId) { DataTable dt1 = new DataTable(); dt1.Columns.AddRange(new DataColumn[] { new DataColumn("message"), new DataColumn("response")}); XmlNodeList nodelist2 = node.SelectNodes("detail")[0].SelectNodes("Detail")[0].ChildNodes; dt1.Rows.Add(nodelist2[0].InnerText, nodelist2[1].InnerText); gv2.DataSource = dt1; gv2.DataBind(); } } } }

    Output:

    Best regards

    Cathy

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 29, 2017 3:23 AM
  • User-148788041 posted
    It works good.how can i do the with xmldocuments instead of xml.
    Wednesday, November 29, 2017 5:11 PM
  • User-148788041 posted

    How suppose i do not want to save Error.xml in a place how can i do it

    Thursday, November 30, 2017 12:59 AM
  • User-707554951 posted

    Hi guhananth,

    Are you want user could upload  Error.xml by themselves?<sub></sub><sup></sup>

    If that the case, you could try to use file upload control 

    e.g: FileUpload control

    Following article I explain a Tutorial with example, how to use the ASP.Net FileUpload control

    You could refer to it:

     https://www.aspsnippets.com/Articles/ASPNet-FileUpload-control-Tutorial-with-example-in-C-and-VBNet.aspx

    Best regards

    Cathy

    Thursday, November 30, 2017 1:57 AM