I have an xml file and its content is as follows. I want to add the information of the flights in this file to the datagridview. The columns I will create in the datagridview are
flight_id, flight_description, flight_date, instructor_num, instructor_name, student_num, student_name
<?xml version="1.0" encoding="utf-8"?>
<Simulator>
<Flight ID="1" Description="sunny" Date="2022-09-09">
<Instructor Num="6">
<Name>matt</Name>
<Surname>matt</Surname>
<Rank>matt</Rank>
</Instructor>
<Student Num="66">
<Name>matt</Name>
<Surname>matt</Surname>
<Rank>matt</Rank>
</Student>
<Operator Num="666">
<Name>matt</Name>
<Surname>matt</Surname>
<Rank>matt</Rank>
</Operator>
<Technician Num="6666">
<Name>matt</Name>
<Surname>matt</Surname>
<Rank>matt</Rank>
</Technician>
<ErrorLOGS>
<Logs Hour_Date="2022-09-09T09:12:00" ID="1">
<ErrorGUI>checkbox</ErrorGUI>
<ErrorType>high</ErrorType>
<ProgramPage>Failures</ProgramPage>
<ErrorStartTime>PT9H12M23S</ErrorStartTime>
</Logs>
<Logs Hour_Date="2022-09-09T09:12:00" ID="1">
<ErrorGUI>checkbox</ErrorGUI>
<ErrorType>high</ErrorType>
<ProgramPage>Page</ProgramPage>
<ErrorStartTime>PT9H12M23S</ErrorStartTime>
</Logs>
</ErrorLOGS>
</Flight>
<Flight ID="2" Description="test flight">
.................like flight1.........
</Flight>
</Simulator>
I tried something like this but failed
string inputpath = Application.StartupPath "\\Data.xml";
XmlDocument doc = new XmlDocument();
doc.LoadXml(inputpath);
IEnumerable<string> names = doc.Root.Descendants("Flight").Select(e => e.Attribute("ID") , e.Attribute("Description"), e.Attribute("Date")).Value);
CodePudding user response:
Try to create classes for the simulator and deserialize it like this
public Simulator FromXml(string value)
{
using (TextReader reader = new StringReader(value))
{
return new XmlSerializer(Simulator).Deserialize(reader);
}
}
public partial class Simulator
{
[JsonProperty("Flight")]
public Flight Flight { get; set; }
}
public partial class Flight
{
[JsonProperty("Instructor")]
public Instructor Instructor { get; set; }
[JsonProperty("Student")]
public Instructor Student { get; set; }
[JsonProperty("Operator")]
public Instructor Operator { get; set; }
[JsonProperty("Technician")]
public Instructor Technician { get; set; }
[JsonProperty("ErrorLOGS")]
public ErrorLogs ErrorLogs { get; set; }
}
public partial class ErrorLogs
{
[JsonProperty("Logs")]
public Log[] Logs { get; set; }
}
public partial class Log
{
[JsonProperty("ErrorGUI")]
public string ErrorGui { get; set; }
[JsonProperty("ErrorType")]
public string ErrorType { get; set; }
[JsonProperty("ProgramPage")]
public string ProgramPage { get; set; }
[JsonProperty("ErrorStartTime")]
public string ErrorStartTime { get; set; }
}
public partial class Instructor
{
[JsonProperty("Name")]
public string Name { get; set; }
[JsonProperty("Surname")]
public string Surname { get; set; }
[JsonProperty("Rank")]
public string Rank { get; set; }
}
CodePudding user response:
Use my previous posting code. Added MakeDataTable. Than you can make the DataSource of dgv the datatable : datagridview1.DataSource = dt.
public class Simulator
{
XDocument doc = null;
public Simulator(string filename)
{
doc = XDocument.Load(filename);
}
public Simulator(string id, string description)
{
string ident = "<?xml version=\"1.0\" encoding=\"utf-8\"?><Simulator></Simulator>";
doc = XDocument.Parse(ident);
XElement flight = new XElement("Flight", new object[] {
new XAttribute("ID", id),
new XAttribute("Description", description)
});
doc.Root.Add(flight);
}
public string AddUser(string flightID, string role, string number, string name, string surname, string rank)
{
XElement flight = doc.Descendants("Flight").Where(x => (string)x.Attribute("ID") == flightID).FirstOrDefault();
if (flight != null)
{
XElement user = new XElement(role, new object[] {
new XAttribute("Num", number),
new XElement("Name", name),
new XElement ("Surname", surname),
new XElement("Rank", rank)
});
flight.Add(user);
return "GOOD";
}
else
{
return "Flight ID doesn't exist";
}
}
public string AddFlight(string flightID, string description)
{
XElement flight = doc.Descendants("Flight").Where(x => (string)x.Attribute("ID") == flightID).FirstOrDefault();
if (flight == null)
{
flight = new XElement("Flight", new object[] {
new XAttribute("ID", flightID),
new XAttribute("Description", description)
});
doc.Root.Add(flight);
return "Good";
}
else
{
return "Flight ID already exists";
}
}
public string AddErrors(string flightID, XElement errorLog)
{
XElement flight = doc.Descendants("Flight").Where(x => (string)x.Attribute("ID") == flightID).FirstOrDefault();
if (flight == null)
{
return "Flight ID doesn't exist";
}
else
{
flight.Add(errorLog);
return "GOOD";
}
}
public void WriteXml(string filename)
{
doc.Save(filename);
}
public DataTable MakeDataTable()
{
DataTable dt = new DataTable();
dt.Columns.Add("flight_id", typeof(int));
dt.Columns.Add("flight_description", typeof(string));
dt.Columns.Add("flight_date",typeof(DateTime));
dt.Columns.Add("instructor_num", typeof(int));
dt.Columns.Add("instructor_name", typeof(string));
dt.Columns.Add("student_num", typeof(int));
dt.Columns.Add("student_name", typeof(string));
foreach (XElement flight in doc.Descendants("Flight"))
{
DataRow row = dt.Rows.Add();
int iD = (int)flight.Attribute("ID");
row["flight_id"] = iD;
string description = (string)flight.Attribute("Description");
row["flight_description"] = description;
DateTime date = (DateTime)flight.Attribute("Date");
row["flight_date"] = date;
XElement xStudent = flight.Element("Student");
if (xStudent != null)
{
int studentNum = (int)xStudent.Attribute("Num");
row["student_num"] = studentNum;
string studentName = (string)xStudent.Element("Name");
row["student_name"] = studentName;
}
XElement xInstructor = flight.Element("Instructor");
if (xInstructor != null)
{
int instructorNum = (int)xStudent.Attribute("Num");
row["instructor_num"] = instructorNum;
string instructorName = (string)xStudent.Element("Name");
row["student_name"] = instructorName;
}
}
return dt;
}
}