How to implement XML Files into Datagrid

akshan 1 Reputation point

So i have a XML File, structure looks like so:
<workout id="3">
I am also creating classes to store the contents of it via tagname like so

public static ArrayList<Workout> workouts = new ArrayList<>();
private final int ID;
private final LocalDateTime start;
private final LocalDateTime end;
private final Equipment equipment;
private final Route route;
private final ArrayList<Waypoint> waypoints;

public Workout(int ID, LocalDateTime start, LocalDateTime end, Equipment equipment, Route route, ArrayList<Waypoint> waypoints) {
    this.ID = ID;
    this.start = start;
    this.end = end; = equipment;
    this.route = route;
    this.waypoints = waypoints;

and i want to implement it in my forms app to show the contents of it
in a Datagridview, but for some reason i cant put it as a Datasource

datagridView1.Datasource = Dataset.readXML("example.xml");

Azure Database Migration service
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
9,937 questions
Windows 10 Compatibility
Windows 10 Compatibility
Windows 10: A Microsoft operating system that runs on personal computers and tablets.Compatibility: The extent to which hardware or software adheres to an accepted standard.
450 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Jack J Jun 24,266 Reputation points Microsoft Vendor

    @akshan , we can not put the XmlReadmodel as the datasource of the Datagirdview. As usual ,we could use the following code to set the datasource from the xml file.

    dataGridView1.DataSource = ds.Tables["Hotel"]; //Tables[0]  

    However, the xml you provided contains the nested nodes, so we can not set the datasouce of the datagirdview directly.

    Here is a code example I write and it could show the data in datagirdview.

    [XmlRoot(ElementName = "waypoint")]  
    	public class Waypoint  
    		[XmlElement(ElementName = "postitionX")]  
    		public double PostitionX { get; set; }  
    		[XmlElement(ElementName = "postitionY")]  
    		public double PostitionY { get; set; }  
    		[XmlElement(ElementName = "speed")]  
    		public double Speed { get; set; }  
    		[XmlElement(ElementName = "heartRate")]  
    		public int HeartRate { get; set; }  
    	[XmlRoot(ElementName = "waypoints")]  
    	public class Waypoints  
    		[XmlElement(ElementName = "waypoint")]  
    		public List<Waypoint> Waypoint { get; set; }  
    	[XmlRoot(ElementName = "workout")]  
    	public class Workout  
    		[XmlElement(ElementName = "start")]  
    		public DateTime Start { get; set; }  
    		[XmlElement(ElementName = "end")]  
    		public DateTime End { get; set; }  
    		[XmlElement(ElementName = "equipmentID")]  
    		public int EquipmentID { get; set; }  
    		[XmlElement(ElementName = "routeID")]  
    		public int RouteID { get; set; }  
    		[XmlElement(ElementName = "waypoints")]  
    		public Waypoints Waypoints { get; set; }  
    		[XmlAttribute(AttributeName = "id")]  
    		public int Id { get; set; }  
    		public string Text { get; set; }  
    	[XmlRoot(ElementName = "workouts")]  
    	public class Workouts  
    		[XmlElement(ElementName = "workout")]  
    		public Workout Workout { get; set; }  
    	[XmlRoot(ElementName = "data")]  
    	public class Data  
    		[XmlElement(ElementName = "workouts")]  
    		public Workouts Workouts { get; set; }  
    private void button1_Click(object sender, EventArgs e)  
    			XmlReader xmlFile = XmlReader.Create("D:\\t.xml", new XmlReaderSettings());  
    			DataSet dataSet = new DataSet();  
                DataTable dtAll = new DataTable();  
                foreach (DataColumn item in dataSet.Tables[1].Columns)  
                foreach (DataColumn item in dataSet.Tables[3].Columns)  
                List<object> arr = new List<object>();  
                foreach (DataRow row in dataSet.Tables[1].Rows)  
                List<object> totalarr = new List<object>();  
                foreach (DataRow row in dataSet.Tables[3].Rows)  
                dataGridView1.DataSource = dtAll;  



    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  2. Limitless Technology 39,181 Reputation points

    Hi there,

    To fill a DataSet with data from XML, use the ReadXml method of the DataSet object. The ReadXml method reads from a file, a stream, or an XmlReader, and takes as arguments the source of the XML plus an optional XmlReadMode argument.

    You can get more info from the Microsft article


    --If the reply is helpful, please Upvote and Accept it as an answer-

    0 comments No comments