@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:
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.