Any way to populate items from xml to Datagridview using c#

后端 未结 3 1955
走了就别回头了
走了就别回头了 2020-12-21 05:49

I\'m working on the datagridview. In which i have to show the values of the column from the xml to the grid view column. I have xml like this:- Also i have a grid view which

相关标签:
3条回答
  • 2020-12-21 06:06

    You can read xml to DataSet and pass DataSet empdetails table to DataGridView like this:

    //Create xml reader
    XmlReader xmlFile = XmlReader.Create("fullPathToYourXmlFile.xml", new XmlReaderSettings());
    DataSet dataSet = new DataSet();
    //Read xml to dataset
    dataSet.ReadXml(xmlFile);
    //Pass empdetails table to datagridview datasource
    dataGridView.DataSource = dataSet.Tables["empdetails"];
    //Close xml reader
    xmlFile.Close();
    
    0 讨论(0)
  • 2020-12-21 06:16
    C#
        DataSet ds = new DataSet();
        ds.ReadXml("C:/XMLData/employee.xml");
        DataGridView1.DataSource = ds.Tables(0);
    
    
    VB.NET
        Dim ds As New DataSet
        ds.ReadXml("C:/XMLData/employee.xml")
        DataGridView1.DataSource = ds.Tables(0)
    
    0 讨论(0)
  • 2020-12-21 06:24

    You can use XML Linq as below

    XElement xml = XElement.Load(XMl String);
    var xmlData = from item in xml.Element("empdetails")                              
                              select new {id = item.Attribute("id") , name= item.Attribute("name")};
    dataGrid.DataSource = xmlData.ToList();
    
    0 讨论(0)
提交回复
热议问题