XMLs in C#, SimBrief XML

I'm trying to extract the name of fixes of a flight plan from an XML file but cannot seem to get it to work. The XML link can be found in the code. The XML snippet is not the whole file there are other nodes and elements.

using System;
using System.Xml;
namespace SBDataTest
{
    public class Program
    {


        static void Main(string[] args)
        {

            XmlDocument fplxml;

            XmlDocument fplxml = new XmlDocument();
            fplxml.LoadXml("https://www.simbrief.com/ofp/flightplans/xml/KEWRMMPR_XML_1520819500.xml");
            var str = fplsml.Parse(fplxml);

            var result = str.Elements("fix").
                            Where(x => x.Element("name")).ToList();

            Console.WriteLine(result);
        }
    }
}

XML File

<?xml version="1.0" encoding="UTF-8"?>
<OFP>
  <navlog>
    <fix>
      <ident>PARKE</ident>
      <name>PARKE</name>
      <type>wpt</type>
      <frequency/>
      <pos_lat>40.683183</pos_lat>
      <pos_long>-75.076478</pos_long>
      <stage>CLB</stage>
      <via_airway>DCT</via_airway>
      <is_sid_star>0</is_sid_star>
      <distance>57</distance>
    </fix>
    <fix>
        <ident>TOWIN</ident>
        <name>TOWIN</name>
        <type>wpt</type>
        <frequency/>
        <pos_lat>40.535211</pos_lat>
        <pos_long>-75.400378</pos_long>
        <stage>CLB</stage>
        <via_airway>J6</via_airway>
        <is_sid_star>0</is_sid_star>
        <distance>17</distance>
    </fix>
  </navlog>
</OFP>

1 answer

  • answered 2018-03-13 22:28 jdweng

    Try xml linq :

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml;
    using System.Xml.Linq;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            const string FILENAME = @"c:\temp\test.xml";
            static void Main(string[] args)
            {
                new Navlog(FILENAME); 
            }
        }
        public class Navlog
        {
            public static List<Navlog> logs { get; set; }
    
            public string ident { get; set; }
            public string name { get; set; }
            public string type { get; set; }
            public decimal? frequency { get; set; }
            public double latitude { get; set; }
            public double longitude { get; set; }
            public string stage { get; set; }
            public string airway { get; set; }
            public string star { get; set; }
            public int distance { get; set; }
    
            public Navlog() { }
            public Navlog(string filename)
            {
                XDocument doc = XDocument.Load(filename);
    
                Navlog.logs = doc.Descendants("fix").Select(x => new Navlog()
                {
                    ident = (string)x.Element("ident"),
                    name = (string)x.Element("name"),
                    type = (string)x.Element("type"),
                    frequency = (string)x.Element("frequency") == "" ? null : (decimal?)x.Element("frequency"),
                    latitude = (double)x.Element("pos_lat"),
                    longitude = (double)x.Element("pos_long"),
                    stage = (string)x.Element("stage"),
                    airway = (string)x.Element("via_airway"),
                    star = (string)x.Element("is_std_star"),
                    distance = (int)x.Element("distance"),
                }).ToList();
            }
    
        }
    }