none
Binding the xml data with datagridview RRS feed

  • Frage

  • 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.

    • Verschoben CoolDadTx Montag, 7. Oktober 2013 14:07 Winforms related
    Montag, 7. Oktober 2013 07:50

Antworten

  • 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

    Montag, 7. Oktober 2013 08:32
  • 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.


    • Bearbeitet ThankfulHeart Montag, 7. Oktober 2013 09:30 Remove the last comma
    • Als Antwort markiert Damon Bu - MSFT Montag, 14. Oktober 2013 09:48
    Montag, 7. Oktober 2013 09:30
  • 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


    Montag, 7. Oktober 2013 11:30

Alle Antworten

  • 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

    Montag, 7. Oktober 2013 08:32
  • 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

    Montag, 7. Oktober 2013 08:58
  • 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.


    • Bearbeitet ThankfulHeart Montag, 7. Oktober 2013 09:30 Remove the last comma
    • Als Antwort markiert Damon Bu - MSFT Montag, 14. Oktober 2013 09:48
    Montag, 7. Oktober 2013 09:30
  • 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


    Montag, 7. Oktober 2013 11:30