question

akshan-8180 avatar image
0 Votes"
akshan-8180 asked JackJJun-MSFT commented

How to implement XML Files into Datagrid

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");

dotnet-csharpwindows-10-application-compatibilityazure-database-migration
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Please let us know if you need more details.

0 Votes 0 ·

@akshan-8180, is any update? Please check if my answer works for you.

0 Votes 0 ·
JackJJun-MSFT avatar image
0 Votes"
JackJJun-MSFT answered

@akshan-8180, 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.


image.png (19.8 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

LimitlessTechnology-2700 avatar image
0 Votes"
LimitlessTechnology-2700 answered

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://docs.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-

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.