none
Binding the xml data with datagridview RRS feed

  • Question

  • Hi,

       I have a problem to bind the xml data to gridview

    My XML:

    <?xml version="1.0" encoding="utf-8"?>
    <users>
      <user>
        <FirstName>Suresh</FirstName>
        <LastName>Dasari</LastName>
        <UserName>SureshDasari</UserName>
        <Job>
          <Role>Team Leader</Role>
        </Job>
        <account>Alltec</account>
      </user>
      <user>
        <FirstName>Mahesh</FirstName>
        <LastName>Dasari</LastName>
        <UserName>MaheshDasari</UserName>
        <Job>
          <Role>SOftware Developer</Role>
        </Job>
        <account>Alltec</account>
      </user>
     
    </users>

    Here I used dataset to ind my xml to the gridview, but its displaying only firstname, lastname, userName and account.

    I want to display role also in that grid.

    please help me out.

    • Moved by CoolDadTx Monday, October 7, 2013 2:07 PM Winforms related
    Monday, October 7, 2013 7:50 AM

Answers

  • Read the xml in DataSet

    and then

    Binding dataset to datagridview

     

    Mark Answered, if it solves your question and Vote if you found it helpful.
    Rohit Arora

    Monday, October 7, 2013 8:32 AM
  • Hello Divya Pannuru:

    If you are using net framework 3.5 (XDocument), it's very easy for you to bind the result to a GridView (suppose you have a GridView instance is called "gv"):

    gv.DataSource= from item in XDocument.Load("Your xml file path").Descendants("user")
                             select new
                             {
                                 FirstName = item.Element("FirstName").Value,
                                 LastName = item.Element("LastName").Value,
                                 UserName = item.Element("UserName").Value,
                                 Job = item.Element("Job").Element("Role").Value,
                                 account = item.Element("account").Value
                             };
    gv.DataBind();

    Click For donating:Free Rice For the poor
    For spamming-sender issues, you can either report it at Microsoft Spamming Issue, or just find "Report Spam Here+Number" at Forum Issue;You can also find "Verify Your Account+Number" at "Forum Issue", where you can submit to be confirmed to paste links or images.
    For more things to talk about? StackOverFlow is your choice.


    • Edited by ThankfulHeart Monday, October 7, 2013 9:30 AM Remove the last comma
    • Marked as answer by Damon Bu - MSFT Monday, October 14, 2013 9:48 AM
    Monday, October 7, 2013 9:30 AM
  • String sXML=String.Empty;
    sXML =" <users>";
    sXML =sXML  + " <user>";
    sXML =sXML  + " <FirstName>Suresh</FirstName>";
    sXML =sXML  + " <LastName>Dasari</LastName>";
    sXML =sXML  + " <UserName>SureshDasari</UserName>";
    sXML =sXML  + " <Job>";
    sXML =sXML  + " <Role>Team Leader</Role>";
    sXML =sXML  + " </Job>";
    sXML =sXML  + " <account>Alltec</account>";
    sXML =sXML  + " </user>";
    sXML =sXML  + " <user>";
    sXML =sXML  + " <FirstName>Mahesh</FirstName>";
    sXML =sXML  + " <LastName>Dasari</LastName>";
    sXML =sXML  + " <UserName>MaheshDasari</UserName>";
    sXML =sXML  + " <Job>";
    sXML =sXML  + " <Role>SOftware Developer</Role>";
    sXML =sXML  + " </Job>";
    sXML =sXML  + " <account>Alltec</account>";
    sXML =sXML  + " </user>";
    
    sXML = sXML + " </users>";
    
    System.IO.StringReader reader = new System.IO.StringReader(sXML);
    DataSet dataset = new DataSet();
    dataset.ReadXml(reader);
    dataGridView1.DataSource = dataset.Tables[0];
     LoadXml String to DataSet and assign the dataset's First Table to DataGridView

    Happy Coding, RDRaja


    Monday, October 7, 2013 11:30 AM

All replies

  • Read the xml in DataSet

    and then

    Binding dataset to datagridview

     

    Mark Answered, if it solves your question and Vote if you found it helpful.
    Rohit Arora

    Monday, October 7, 2013 8:32 AM
  • I believe you need to specify some template as "Role" is nested in another element, it's not likely Microsoft can do this automatically for you.

    http://msdn.microsoft.com/en-us/library/bb288032.aspx


    Cheers, Daniel

    Monday, October 7, 2013 8:58 AM
  • Hello Divya Pannuru:

    If you are using net framework 3.5 (XDocument), it's very easy for you to bind the result to a GridView (suppose you have a GridView instance is called "gv"):

    gv.DataSource= from item in XDocument.Load("Your xml file path").Descendants("user")
                             select new
                             {
                                 FirstName = item.Element("FirstName").Value,
                                 LastName = item.Element("LastName").Value,
                                 UserName = item.Element("UserName").Value,
                                 Job = item.Element("Job").Element("Role").Value,
                                 account = item.Element("account").Value
                             };
    gv.DataBind();

    Click For donating:Free Rice For the poor
    For spamming-sender issues, you can either report it at Microsoft Spamming Issue, or just find "Report Spam Here+Number" at Forum Issue;You can also find "Verify Your Account+Number" at "Forum Issue", where you can submit to be confirmed to paste links or images.
    For more things to talk about? StackOverFlow is your choice.


    • Edited by ThankfulHeart Monday, October 7, 2013 9:30 AM Remove the last comma
    • Marked as answer by Damon Bu - MSFT Monday, October 14, 2013 9:48 AM
    Monday, October 7, 2013 9:30 AM
  • String sXML=String.Empty;
    sXML =" <users>";
    sXML =sXML  + " <user>";
    sXML =sXML  + " <FirstName>Suresh</FirstName>";
    sXML =sXML  + " <LastName>Dasari</LastName>";
    sXML =sXML  + " <UserName>SureshDasari</UserName>";
    sXML =sXML  + " <Job>";
    sXML =sXML  + " <Role>Team Leader</Role>";
    sXML =sXML  + " </Job>";
    sXML =sXML  + " <account>Alltec</account>";
    sXML =sXML  + " </user>";
    sXML =sXML  + " <user>";
    sXML =sXML  + " <FirstName>Mahesh</FirstName>";
    sXML =sXML  + " <LastName>Dasari</LastName>";
    sXML =sXML  + " <UserName>MaheshDasari</UserName>";
    sXML =sXML  + " <Job>";
    sXML =sXML  + " <Role>SOftware Developer</Role>";
    sXML =sXML  + " </Job>";
    sXML =sXML  + " <account>Alltec</account>";
    sXML =sXML  + " </user>";
    
    sXML = sXML + " </users>";
    
    System.IO.StringReader reader = new System.IO.StringReader(sXML);
    DataSet dataset = new DataSet();
    dataset.ReadXml(reader);
    dataGridView1.DataSource = dataset.Tables[0];
     LoadXml String to DataSet and assign the dataset's First Table to DataGridView

    Happy Coding, RDRaja


    Monday, October 7, 2013 11:30 AM