주 콘텐츠로 건너뛰기

 none
Binding the xml data with datagridview RRS feed

  • 질문

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

    • 이동됨 CoolDadTx 2013년 10월 7일 월요일 오후 2:07 Winforms related
    2013년 10월 7일 월요일 오전 7:50

답변

  • 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

    • 답변으로 표시됨 Damon Bu - MSFT 2013년 10월 14일 월요일 오전 9:48
    2013년 10월 7일 월요일 오전 8: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.


    • 편집됨 ThankfulHeart 2013년 10월 7일 월요일 오전 9:30 Remove the last comma
    • 답변으로 표시됨 Damon Bu - MSFT 2013년 10월 14일 월요일 오전 9:48
    2013년 10월 7일 월요일 오전 9: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


    2013년 10월 7일 월요일 오전 11:30

모든 응답

  • 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

    • 답변으로 표시됨 Damon Bu - MSFT 2013년 10월 14일 월요일 오전 9:48
    2013년 10월 7일 월요일 오전 8: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

    2013년 10월 7일 월요일 오전 8: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.


    • 편집됨 ThankfulHeart 2013년 10월 7일 월요일 오전 9:30 Remove the last comma
    • 답변으로 표시됨 Damon Bu - MSFT 2013년 10월 14일 월요일 오전 9:48
    2013년 10월 7일 월요일 오전 9: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


    2013년 10월 7일 월요일 오전 11:30