Parse Xml document to windows application c#

Alpha Beta 41 Reputation points
2021-08-27T04:48:20.55+00:00

Hello Everyone!

I have been trying to pull this out by myself but couldn't.
Xml document needs to be parsed into windows application form in c# either using Xdocument or Linq and i want to display this as data table in windows application

My XML Code:

<AvailabilityResponse>
<TrackId>RAB25B</TrackId>
<ItinearyDetails>
<Items>
<FlightDetails>
<FlightID>8120</FlightID>
<CarrierCode>6E</CarrierCode>
<Origin>DXB</Origin>
<Destination>MLE</Destination>
<FlightNum>8246</FlightNum>
<DepartureDateTime>04/08/2021 09:10</DepartureDateTime>
<ArrivalDateTime>04/08/2021 12:45</ArrivalDateTime>
<Duration>04Hrs5Mins</Duration>
<OrgTerminal>1</OrgTerminal>
<DesTerminal />
<AirCraftType>320</AirCraftType>
<NumberofStops>0</NumberofStops>
<Via />
<ValidatingCarrier>6E</ValidatingCarrier>
<AirlineCategory>LCC</AirlineCategory>
<ItryNo>0</ItryNo>
<FareType>Y</FareType>
<AllowBaggage>20 KG</AllowBaggage>
<Seats>9</Seats>
<ClassCode>K</ClassCode>
<ClassCodeDesc>REFUNDABLE</ClassCodeDesc>
<FareBasisCode>K0INTRT</FareBasisCode>
<BreakPoint />
<CurrencyCode>INR</CurrencyCode>
</FlightDetails>
<FlightDetails>
<FlightID>8121</FlightID>
<CarrierCode>6E</CarrierCode>
<Origin>MLE</Origin>
<Destination>DXB</Destination>
<FlightNum>8247</FlightNum>
<DepartureDateTime>07/08/2021 13:50</DepartureDateTime>
<ArrivalDateTime>07/08/2021 18:25</ArrivalDateTime>
<Duration>04Hrs5Mins</Duration>
<OrgTerminal />
<DesTerminal>3</DesTerminal>
<AirCraftType>320</AirCraftType>
<NumberofStops>0</NumberofStops>
<Via />
<ValidatingCarrier>6E</ValidatingCarrier>
<AirlineCategory>LCC</AirlineCategory>
<ItryNo>1</ItryNo>
<FareType>N</FareType>
<AllowBaggage>20 KG</AllowBaggage>
<Seats>9</Seats>
<ClassCode>H</ClassCode>
<ClassCodeDesc>REFUNDABLE</ClassCodeDesc>
<FareBasisCode>H0INT</FareBasisCode>
<BreakPoint />
<CurrencyCode>INR</CurrencyCode>
</FlightDetails>
<FareDescription>
<PaxFareDetails>
<PaxType>ADT</PaxType>
<BasicAmount>22915.00</BasicAmount>
<TotalTaxAmount>5511.50</TotalTaxAmount>
<FuelSurcharge>0.00</FuelSurcharge>
<Commission>0.00</Commission>
<OtherInfo>
<Item>
<TaxCode>TAX</TaxCode>
<Amount>5499.00</Amount>
</Item>
<Item>
<TaxCode>SC </TaxCode>
<Amount>12.50</Amount>
</Item>
<TransactionFee>0.00</TransactionFee>
<ServiceTax>0.00</ServiceTax>
<GrossAmount>28426.50</GrossAmount>
</OtherInfo>
</PaxFareDetails>
</FareDescription>
</Items>
<Items>
<FlightDetails>
<FlightID>8122</FlightID>
<CarrierCode>6E</CarrierCode>
<Origin>DXB</Origin>
<Destination>MLE</Destination>
<FlightNum>8246</FlightNum>
<DepartureDateTime>04/08/2021 09:10</DepartureDateTime>
<ArrivalDateTime>04/08/2021 12:45</ArrivalDateTime>
<Duration>04Hrs5Mins</Duration>
<OrgTerminal>1</OrgTerminal>
<DesTerminal />
<AirCraftType>320</AirCraftType>
<NumberofStops>0</NumberofStops>
<Via />
<ValidatingCarrier>6E</ValidatingCarrier>
<AirlineCategory>LCC</AirlineCategory>
<ItryNo>0</ItryNo>
<FareType>Y</FareType>
<AllowBaggage>20 KG</AllowBaggage>
<Seats>9</Seats>
<ClassCode>K</ClassCode>
<ClassCodeDesc>REFUNDABLE</ClassCodeDesc>
<FareBasisCode>K0INTRT</FareBasisCode>
<BreakPoint />
<CurrencyCode>INR</CurrencyCode>
</FlightDetails>
<FlightDetails>
<FlightID>8123</FlightID>
<CarrierCode>6E</CarrierCode>
<Origin>MLE</Origin>
<Destination>DXB</Destination>
<FlightNum>8247</FlightNum>
<DepartureDateTime>07/08/2021 13:50</DepartureDateTime>
<ArrivalDateTime>07/08/2021 18:25</ArrivalDateTime>
<Duration>04Hrs5Mins</Duration>
<OrgTerminal />
<DesTerminal>3</DesTerminal>
<AirCraftType>320</AirCraftType>
<NumberofStops>0</NumberofStops>
<Via />
<ValidatingCarrier>6E</ValidatingCarrier>
<AirlineCategory>LCC</AirlineCategory>
<ItryNo>1</ItryNo>
<FareType>Y</FareType>
<AllowBaggage>20 KG</AllowBaggage>
<Seats>9</Seats>
<ClassCode>H</ClassCode>
<ClassCodeDesc>REFUNDABLE</ClassCodeDesc>
<FareBasisCode>H0INTRT</FareBasisCode>
<BreakPoint />
<CurrencyCode>INR</CurrencyCode>
</FlightDetails>
<FareDescription>
<PaxFareDetails>
<PaxType>ADT</PaxType>
<BasicAmount>25147.00</BasicAmount>
<TotalTaxAmount>5511.50</TotalTaxAmount>
<FuelSurcharge>0.00</FuelSurcharge>
<Commission>0.00</Commission>
<OtherInfo>
<Item>
<TaxCode>TAX</TaxCode>
<Amount>5499.00</Amount>
</Item>
<Item>
<TaxCode>SC </TaxCode>
<Amount>12.50</Amount>
</Item>
<TransactionFee>0.00</TransactionFee>
<ServiceTax>0.00</ServiceTax>
<GrossAmount>30658.50</GrossAmount>
</OtherInfo>
</PaxFareDetails>
</FareDescription>
</Items>
<Items>
<FlightDetails>
<FlightID>8124</FlightID>
<CarrierCode>6E</CarrierCode>
<Origin>DXB</Origin>
<Destination>MLE</Destination>
<FlightNum>8246</FlightNum>
<DepartureDateTime>04/08/2021 09:10</DepartureDateTime>
<ArrivalDateTime>04/08/2021 12:45</ArrivalDateTime>
<Duration>04Hrs5Mins</Duration>
<OrgTerminal>1</OrgTerminal>
<DesTerminal />
<AirCraftType>320</AirCraftType>
<NumberofStops>0</NumberofStops>
<Via />
<ValidatingCarrier>6E</ValidatingCarrier>
<AirlineCategory>LCC</AirlineCategory>
<ItryNo>0</ItryNo>
<FareType>Y</FareType>
<AllowBaggage>20 KG</AllowBaggage>
<Seats>9</Seats>
<ClassCode>K</ClassCode>
<ClassCodeDesc>REFUNDABLE</ClassCodeDesc>
<FareBasisCode>K0INTRT</FareBasisCode>
<BreakPoint />
<CurrencyCode>INR</CurrencyCode>
</FlightDetails>
<FlightDetails>
<FlightID>8125</FlightID>
<CarrierCode>6E</CarrierCode>
<Origin>MLE</Origin>
<Destination>COK</Destination>
<FlightNum>1796</FlightNum>
<DepartureDateTime>07/08/2021 15:50</DepartureDateTime>
<ArrivalDateTime>07/08/2021 17:50</ArrivalDateTime>
<Duration>01Hrs30Mins</Duration>
<OrgTerminal />
<DesTerminal>3</DesTerminal>
<AirCraftType>320</AirCraftType>
<NumberofStops>1</NumberofStops>
<Via />
<ValidatingCarrier>6E</ValidatingCarrier>
<AirlineCategory>LCC</AirlineCategory>
<ItryNo>1</ItryNo>
<FareType>S</FareType>
<AllowBaggage>20 KG</AllowBaggage>
<Seats>9</Seats>
<ClassCode>E</ClassCode>
<ClassCodeDesc>REFUNDABLE</ClassCodeDesc>
<FareBasisCode>E0INT</FareBasisCode>
<BreakPoint />
<CurrencyCode>INR</CurrencyCode>
</FlightDetails>
<FlightDetails>
<FlightID>8126</FlightID>
<CarrierCode>6E</CarrierCode>
<Origin>COK</Origin>
<Destination>DXB</Destination>
<FlightNum>706</FlightNum>
<DepartureDateTime>08/08/2021 10:00</DepartureDateTime>
<ArrivalDateTime>08/08/2021 13:15</ArrivalDateTime>
<Duration>03Hrs15Mins</Duration>
<OrgTerminal>1</OrgTerminal>
<DesTerminal>1</DesTerminal>
<AirCraftType>320</AirCraftType>
<NumberofStops>1</NumberofStops>
<Via />
<ValidatingCarrier>6E</ValidatingCarrier>
<AirlineCategory>LCC</AirlineCategory>
<ItryNo>1</ItryNo>
<FareType>S</FareType>
<AllowBaggage>20 KG</AllowBaggage>
<Seats>9</Seats>
<ClassCode>R</ClassCode>
<ClassCodeDesc>REFUNDABLE</ClassCodeDesc>
<FareBasisCode>R8TCT</FareBasisCode>
<BreakPoint />
<CurrencyCode>INR</CurrencyCode>
</FlightDetails>
<FareDescription>
<PaxFareDetails>
<PaxType>ADT</PaxType>
<BasicAmount>22771.00</BasicAmount>
<TotalTaxAmount>5561.50</TotalTaxAmount>
<FuelSurcharge>0.00</FuelSurcharge>
<Commission>0.00</Commission>
<OtherInfo>
<Item>
<TaxCode>TAX</TaxCode>
<Amount>5549.00</Amount>
</Item>
<Item>
<TaxCode>SC </TaxCode>
<Amount>12.50</Amount>
</Item>
<TransactionFee>0.00</TransactionFee>
<ServiceTax>0.00</ServiceTax>
<GrossAmount>28332.50</GrossAmount>
</OtherInfo>
</PaxFareDetails>
</FareDescription>
</Items>
</ItinearyDetails>
</AvailabilityResponse>

What I have tried so far :

XmlDocument xdoc = new XmlDocument();
xdoc.Load("E:\MCA LATEST SOURCE\AvailabilityResponse.xml");
foreach(XmlNode xNode in xdoc.SelectNodes("AvailabilityResponse/ItinearyDetails/Items/FlightDetails"))
{
ListViewItem lvi = new ListViewItem(xNode.SelectSingleNode("FlightID").InnerText);
lvi.SubItems.Add(xNode.SelectSingleNode("CarrierCode").InnerText);
lvi.SubItems.Add(xNode.SelectSingleNode("Origin").InnerText);
lvi.SubItems.Add(xNode.SelectSingleNode("Destination").InnerText);
lvi.SubItems.Add(xNode.SelectSingleNode("FlightNum").InnerText);
lvi.SubItems.Add(xNode.SelectSingleNode("DepartureDateTime").InnerText);
lvi.SubItems.Add(xNode.SelectSingleNode("ArrivalDateTime").InnerText);
lvi.SubItems.Add(xNode.SelectSingleNode("Duration").InnerText);
lvi.SubItems.Add(xNode.SelectSingleNode("OrgTerminal").InnerText);
lvi.SubItems.Add(xNode.SelectSingleNode("DesTerminal").InnerText);
lvi.SubItems.Add(xNode.SelectSingleNode("AirCraftType").InnerText);
lvi.SubItems.Add(xNode.SelectSingleNode("NumberofStops").InnerText);
lvi.SubItems.Add(xNode.SelectSingleNode("ValidatingCarrier").InnerText);
lvi.SubItems.Add(xNode.SelectSingleNode("AirlineCategory").InnerText);
lvi.SubItems.Add(xNode.SelectSingleNode("ItryNo").InnerText);
lvi.SubItems.Add(xNode.SelectSingleNode("FareType").InnerText);
lvi.SubItems.Add(xNode.SelectSingleNode("AllowBaggage").InnerText);
lvi.SubItems.Add(xNode.SelectSingleNode("Seats").InnerText);
lvi.SubItems.Add(xNode.SelectSingleNode("ClassCode").InnerText);
lvi.SubItems.Add(xNode.SelectSingleNode("ClassCodeDesc").InnerText);
lvi.SubItems.Add(xNode.SelectSingleNode("FareBasisCode").InnerText);
lvi.SubItems.Add(xNode.SelectSingleNode("CurrencyCode").InnerText);

                    foreach ( XmlNode xNode1 in xdoc.SelectNodes("AvailabilityResponse/ItinearyDetails/Items/FareDescription/PaxFareDetails"))
                    {
                        lvi.SubItems.Add(xNode1.SelectSingleNode("PaxType").InnerText);
                        lvi.SubItems.Add(xNode1.SelectSingleNode("BasicAmount").InnerText);
                        lvi.SubItems.Add(xNode1.SelectSingleNode("TotalTaxAmount").InnerText);
                        lvi.SubItems.Add(xNode1.SelectSingleNode("FuelSurcharge").InnerText);
                        lvi.SubItems.Add(xNode1.SelectSingleNode("Commission").InnerText);

                        break;
                   }

                    foreach (XmlNode xNode2 in xdoc.SelectNodes("AvailabilityResponse/ItinearyDetails/Items/FareDescription/PaxFareDetails/OtherInfo/Item"))
                    {

                        lvi.SubItems.Add(xNode2.SelectSingleNode("TaxCode").InnerText);                            
                        lvi.SubItems.Add(xNode2.SelectSingleNode("Amount").InnerText);

                        break;
                    }

                    foreach (XmlNode xNode3 in xdoc.SelectNodes("AvailabilityResponse/ItinearyDetails/Items/FareDescription/PaxFareDetails/OtherInfo/Item"))
                    {
                        lvi.SubItems.Add(xNode3.SelectSingleNode("TaxCode").InnerText);
                        lvi.SubItems.Add(xNode3.SelectSingleNode("Amount").InnerText);

                        break;
                    }

                    foreach (XmlNode xNode4 in xdoc.SelectNodes("AvailabilityResponse/ItinearyDetails/Items/FareDescription/PaxFareDetails/OtherInfo"))
                    {
                        lvi.SubItems.Add(xNode4.SelectSingleNode("TransactionFee").InnerText);
                        lvi.SubItems.Add(xNode4.SelectSingleNode("ServiceTax").InnerText);
                        lvi.SubItems.Add(xNode4.SelectSingleNode("GrossAmount").InnerText);

                        break;
                    }
                listView1.Items.Add(lvi);
            }

Can you guys help out !
Thanks in advance.

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,865 questions
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,512 questions
0 comments No comments
{count} votes

Accepted answer
  1. Jack J Jun 24,476 Reputation points Microsoft Vendor
    2021-08-27T08:47:56.967+00:00

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

    127064-image.png


    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.


0 additional answers

Sort by: Most helpful