How to implement XML Files into Datagrid

akshan 1 Reputation point
2021-12-06T05:59:19.607+00:00

So i have a XML File, structure looks like so:
<data>
<workouts>
...
<workout id="3">
<start>2021-11-22T07:10:34</start>
<end>2021-11-22T08:00:00</end>
<equipmentID>1</equipmentID>
<routeID>1</routeID>
<waypoints>
<waypoint>
<postitionX>48.24912599533484</postitionX>
<postitionY>14.491311098297038</postitionY>
<speed>23.56</speed>
<heartRate>98</heartRate>
</waypoint>
<waypoint>
<postitionX>48.240909352357015</postitionX>
<postitionY>14.516923658668736</postitionY>
<speed>24.67</speed>
<heartRate>101</heartRate>
</waypoint>
<waypoint>
<postitionX>48.246650516406596</postitionX>
<postitionY>14.547931919447299</postitionY>
<speed>25.34</speed>
<heartRate>104</heartRate>
</waypoint>
<waypoint>
<postitionX>48.226691625170645</postitionX>
<postitionY>14.58215015497887</postitionY>
<speed>24.56</speed>
<heartRate>108</heartRate>
</waypoint>
<waypoint>
<postitionX>48.23400548108094</postitionX>
<postitionY>14.622705354471632</postitionY>
<speed>25.57</speed>
<heartRate>115</heartRate>
</waypoint>
<waypoint>
<postitionX>48.239594794658636</postitionX>
<postitionY>14.640244022864664</postitionY>
<speed>22.45</speed>
<heartRate>117</heartRate>
</waypoint>
</waypoints>
</workout>
</workouts>
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;
    this.equipment = equipment;
    this.route = route;
    this.waypoints = waypoints;
    workouts.add(this);
}

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
C#
C#
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.
10,277 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.
456 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Jack J Jun 24,296 Reputation points Microsoft Vendor
    2021-12-06T09:15:03.587+00:00

    @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; }  
      
    		[XmlText]  
    		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();  
                dataSet.ReadXml(xmlFile);  
                DataTable dtAll = new DataTable();  
                foreach (DataColumn item in dataSet.Tables[1].Columns)  
                {  
                    dtAll.Columns.Add(item.ColumnName);  
                }  
                foreach (DataColumn item in dataSet.Tables[3].Columns)  
                {  
                    dtAll.Columns.Add(item.ColumnName);  
                }  
                List<object> arr = new List<object>();  
                foreach (DataRow row in dataSet.Tables[1].Rows)  
                {  
                    arr=row.ItemArray.ToList();  
                }  
                List<object> totalarr = new List<object>();  
      
                foreach (DataRow row in dataSet.Tables[3].Rows)  
                {  
                    totalarr=arr.Concat(row.ItemArray).ToList();  
                    dtAll.Rows.Add(totalarr.ToArray());  
                }  
                dataGridView1.DataSource = dtAll;  
                xmlFile.Close();  
            }  
    

    Result:

    155281-image.png


    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,371 Reputation points
    2021-12-09T13:42:21.517+00:00

    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 https://learn.microsoft.com/en-us/dotnet/framework/data/adonet/dataset-datatable-dataview/loading-a-dataset-from-xml

    ---------------------------------------------------------------------------------------------------------------------------------------------------------------

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

    0 comments No comments