@Alpha Beta , it is hard to convert this xml file to a datatale or show it in a listview. Because your xml file has nested nodes, they can not One-to-one correspondence.
Therefore, I recommend that you use two listviews to show the xml.
Here is a code example you could refer to.
private void button1_Click(object sender, EventArgs e)
{
string path = "D:\\1.xml";
XDocument xdoc = XDocument.Load(path);
listView1.View = View.Details;
listView2.View = View.Details;
List<string> columnnames = new List<string>();
var names1 = xdoc.Descendants("FlightDetails").Elements().Where(i=>i.Value!=""&& i.HasElements == false).Select(t =>t.Name.LocalName).Distinct();
var names2= xdoc.Descendants("PaxFareDetails").Elements().Where(i => i.Value!= ""&&i.HasElements==false).Select(t => t.Name.LocalName).Distinct();
var names3 = xdoc.Descendants("Item").Elements().Where(i => i.Value != "" && i.HasElements == false).Select(t => t.Name.LocalName).Distinct();
var names4 = xdoc.Descendants("OtherInfo").Elements().Where(i => i.Value != "" && i.HasElements == false).Select(t => t.Name.LocalName).Distinct();
columnnames.AddRange(names2);
columnnames.AddRange(names3);
columnnames.AddRange(names4);
names1.ToList().ForEach(name => listView1.Columns.Add(name));
columnnames.ToList().ForEach(name => listView2.Columns.Add(name));
foreach (var xNode in xdoc.Descendants("FlightDetails"))
{
Console.WriteLine(xNode.Element("FlightID").Value);
ListViewItem lvi = new ListViewItem(xNode.Element("FlightID").Value);
lvi.SubItems.Add(xNode.Element("FlightID").Value);
lvi.SubItems.Add(xNode.Element("Origin").Value);
lvi.SubItems.Add(xNode.Element("Destination").Value);
lvi.SubItems.Add(xNode.Element("FlightNum").Value);
lvi.SubItems.Add(xNode.Element("DepartureDateTime").Value);
lvi.SubItems.Add(xNode.Element("ArrivalDateTime").Value);
lvi.SubItems.Add(xNode.Element("Duration").Value);
lvi.SubItems.Add(xNode.Element("Duration").Value);
lvi.SubItems.Add(xNode.Element("OrgTerminal").Value);
lvi.SubItems.Add(xNode.Element("DesTerminal").Value);
lvi.SubItems.Add(xNode.Element("AirCraftType").Value);
lvi.SubItems.Add(xNode.Element("NumberofStops").Value);
lvi.SubItems.Add(xNode.Element("ValidatingCarrier").Value);
lvi.SubItems.Add(xNode.Element("AirlineCategory").Value);
lvi.SubItems.Add(xNode.Element("ItryNo").Value);
lvi.SubItems.Add(xNode.Element("FareType").Value);
lvi.SubItems.Add(xNode.Element("AllowBaggage").Value);
lvi.SubItems.Add(xNode.Element("Seats").Value);
lvi.SubItems.Add(xNode.Element("ClassCode").Value);
lvi.SubItems.Add(xNode.Element("ClassCodeDesc").Value);
lvi.SubItems.Add(xNode.Element("FareBasisCode").Value);
lvi.SubItems.Add(xNode.Element("CurrencyCode").Value);
listView1.Items.Add(lvi);
}
foreach (var item in xdoc.Descendants("PaxFareDetails"))
{
ListViewItem lvi = new ListViewItem(item.Element("PaxType").Value);
lvi.SubItems.Add(item.Element("Commission").Value);
lvi.SubItems.Add(item.Element("BasicAmount").Value);
lvi.SubItems.Add(item.Element("TotalTaxAmount").Value);
lvi.SubItems.Add(item.Element("FuelSurcharge").Value);
lvi.SubItems.Add(item.Element("OtherInfo").Element("Item").Element("TaxCode").Value);
lvi.SubItems.Add(item.Element("OtherInfo").Element("Item").Element("Amount").Value);
lvi.SubItems.Add(item.Element("OtherInfo").Element("TransactionFee").Value);
lvi.SubItems.Add(item.Element("OtherInfo").Element("ServiceTax").Value);
lvi.SubItems.Add(item.Element("OtherInfo").Element("GrossAmount").Value);
listView2.Items.Add(lvi);
}
}
Result:
If the response is helpful, please click "Accept Answer" and upvote it.
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.