解析使用C#类生成的XML

时间:2017-02-25 14:11:32

标签: c# xml xml-parsing uwp

我有一个我要解析的大型XML文件。我访问了here并为此生成了相关的C#代码。[没有粘贴完整的XML,因为它有大约30多个类]。

  /* 
    Licensed under the Apache License, Version 2.0

    http://www.apache.org/licenses/LICENSE-2.0
    */
using System;
using System.Xml.Serialization;
using System.Collections.Generic;
namespace Xml2CSharp
{
    [XmlRoot(ElementName="M633Header", Namespace="http://aeec.aviation-ia.net/633")]
    public class M633Header {
        [XmlAttribute(AttributeName="timestamp")]
        public string Timestamp { get; set; }
        [XmlAttribute(AttributeName="versionNumber")]
        public string VersionNumber { get; set; }
    }

    [XmlRoot(ElementName="FlightNumber", Namespace="http://aeec.aviation-ia.net/633")]
    public class FlightNumber {
        [XmlElement(ElementName="CommercialFlightNumber", Namespace="http://aeec.aviation-ia.net/633")]
        public string CommercialFlightNumber { get; set; }
        [XmlAttribute(AttributeName="airlineIATACode")]
        public string AirlineIATACode { get; set; }
        [XmlAttribute(AttributeName="number")]
        public string Number { get; set; }
    }

    [XmlRoot(ElementName="FlightIdentification", Namespace="http://aeec.aviation-ia.net/633")]
    public class FlightIdentification {
        [XmlElement(ElementName="FlightIdentifier", Namespace="http://aeec.aviation-ia.net/633")]
        public string FlightIdentifier { get; set; }
        [XmlElement(ElementName="FlightNumber", Namespace="http://aeec.aviation-ia.net/633")]
        public FlightNumber FlightNumber { get; set; }
    }

    [XmlRoot(ElementName="DepartureAirport", Namespace="http://aeec.aviation-ia.net/633")]
    public class DepartureAirport {
        [XmlElement(ElementName="AirportICAOCode", Namespace="http://aeec.aviation-ia.net/633")]
        public string AirportICAOCode { get; set; }
        [XmlElement(ElementName="AirportIATACode", Namespace="http://aeec.aviation-ia.net/633")]
        public string AirportIATACode { get; set; }
        [XmlAttribute(AttributeName="airportFunction")]
        public string AirportFunction { get; set; }
        [XmlAttribute(AttributeName="airportName")]
        public string AirportName { get; set; }
    }

我正在考虑使用单一方法解析所有数据的方法(如果可能)。 我能想到的是这个

private  void button_Click(object sender, RoutedEventArgs e)
{

    string XMLFilePath = Path.Combine(Package.Current.InstalledLocation.Path, "UTC34.xml");
    //string data = await FileIO.ReadTextAsync(file);

    if(File.Exists(XMLFilePath))
    {

        FileStream fs = new FileStream(XMLFilePath, FileMode.Open, FileAccess.Read);

        TextReader ff = new StreamReader(fs);
        XDocument xmldoc = XDocument.Parse(ff.ReadToEnd());
        XmlSerializer ser1 = new XmlSerializer(typeof(FlightPlan));
        using (var reader = xmldoc.CreateReader())
        {
            var serializer = new XmlSerializer(typeof(FlightPlan));
            FlightPlan someTest = serializer.Deserialize(reader) as FlightPlan;
            // NotSupportedException here (as inner exception)
        }
    }
}

上面的解析代码可能只解析FlightPlan类的所有数据以及我需要编写方法的其他类(普通任务)。
任何人都可以用任何其他方式帮助我减少我的努力吗?

0 个答案:

没有答案