奇怪的反序列化问题

时间:2016-03-01 22:45:18

标签: c# xml xsd xml-deserialization

我已经使用xsd.exe从XSD Schema创建了类(也尝试使用xsd2code,它们以一种立即工作的方式获得更好的结果,并且使用xsd.exe我必须调试一些错误)。我使用的XSD架构可以在http://www.landxml.org/schema/LandXML-1.2/LandXML-1.2.xsd找到,样本文件可以在http://landxml.org/schema/LandXML-1.1/samples/AASHTO%20SDMS/MntnRoad.xml找到。

我的反序列化代码如下:

var mySerializer = new XmlSerializer(typeof (LandXML), new XmlRootAttribute(""));
TextReader myFileStream = new StreamReader("myFile.xml");
var myObject = (LandXML) mySerializer.Deserialize(myFileStream);

我的问题是反序列化的结果是XmlElement类型的项目列表,所以如果我尝试访问它们的属性,我就不能轻易做到这一点。如果我想访问myFile.xml中的某些Alignment对象属性,则代码与此类似:

 var a = myObject.Items[5];
 var b = (XmlElement) a;
 var c = b.ChildNodes.Item(5).ChildNodes.Item(0).ChildNodes.Item(0).Attributes[0].Value;

很明显,这不是将XML反序列化为类时的方法。我的想法就像(对于同一个元素):

var c = LandXML.Alignments.Alignment.CoordGeometry.Curve.rot

我不知道我做错了什么,我尝试过更简单的模式,这段代码运行良好。请提前帮助和tnx!

编辑1

这是我班上的顶级,我认为这种List类型会产生麻烦。在我生成的类中有一个更相似的代码

public class LandXML
    {

        private List<object> _items;

        private System.DateTime _date;

        private System.DateTime _time;

        private string _version;

        private string _language;

        private bool _readOnly;

        private int _landXMLId;

        private string _crc;

        public LandXML()
        {
            this._items = new List<object>();
        }

        [System.Xml.Serialization.XmlAnyElementAttribute()]
        [System.Xml.Serialization.XmlElementAttribute("Alignments", typeof(Alignments))]
        [System.Xml.Serialization.XmlElementAttribute("Amendment", typeof(Amendment))]
        [System.Xml.Serialization.XmlElementAttribute("Application", typeof(Application))]
        [System.Xml.Serialization.XmlElementAttribute("CgPoints", typeof(CgPoints))]
        [System.Xml.Serialization.XmlElementAttribute("CoordinateSystem", typeof(CoordinateSystem))]
        [System.Xml.Serialization.XmlElementAttribute("FeatureDictionary", typeof(FeatureDictionary))]
        [System.Xml.Serialization.XmlElementAttribute("GradeModel", typeof(GradeModel))]
        [System.Xml.Serialization.XmlElementAttribute("Monuments", typeof(Monuments))]
        [System.Xml.Serialization.XmlElementAttribute("Parcels", typeof(Parcels))]
        [System.Xml.Serialization.XmlElementAttribute("PipeNetworks", typeof(PipeNetworks))]
        [System.Xml.Serialization.XmlElementAttribute("PlanFeatures", typeof(PlanFeatures))]
        [System.Xml.Serialization.XmlElementAttribute("Project", typeof(Project))]
        [System.Xml.Serialization.XmlElementAttribute("Roadways", typeof(Roadways))]
        [System.Xml.Serialization.XmlElementAttribute("Surfaces", typeof(Surfaces))]
        [System.Xml.Serialization.XmlElementAttribute("Survey", typeof(Survey))]
        [System.Xml.Serialization.XmlElementAttribute("Units", typeof(Units))]
        public List<object> Items
        {
            get
            {
                return this._items;
            }
            set
            {
                this._items = value;
            }
        }

1 个答案:

答案 0 :(得分:0)

试试这个

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XmlSerializer xs = new XmlSerializer(typeof(LandXML));
            XmlTextReader reader = new XmlTextReader(FILENAME);
            reader.Namespaces = false;
            LandXML landXML = (LandXML)xs.Deserialize(reader);
        }
    }

    [XmlRoot("LandXML")]
    public class LandXML
    {
        [XmlAttribute("version")]
        public double version  { get;set; }

        [XmlAttribute("date")]
        public DateTime date  { get;set; }

        [XmlAttribute("time")]
        public DateTime time { get; set; }

        [XmlAttribute("readOnly")]
        public Boolean readOnly  { get;set; }

        [XmlAttribute("language")]
        public string language  { get;set; }

        [XmlElement("Project")]
        public Project project { get; set; } 

        [XmlElement("Units")]
        public Units units { get; set; } 

        [XmlElement("Application")]
        public Application application { get; set; } 

        [XmlElement("Alignments")]
        public Alignments alignments { get; set; } 
}
    [XmlRoot("Project")]
    public class Project
    {
        [XmlAttribute("name")]
        public string name;
    }
    [XmlRoot("Units")]
    public class Units
    {
        [XmlElement("Imperial")]
        public Imperial imperial { get; set; } 
    }
    [XmlRoot("Application")]
    public class Application
    {
        [XmlElement("Author")]
        public Author author { get; set; } 
    }
    [XmlRoot("Imperial")]
    public class Imperial
    {
        [XmlAttribute("linearUnit")]
        public string linearUnit;

        [XmlAttribute("areaUnit")]
        public string areaUnit;

        [XmlAttribute("volumeUnit")]
        public string volumeUnit;

        [XmlAttribute("temperatureUnit")]
        public string temperaturUnit;

        [XmlAttribute("pressureUnit")]
        public string pressureUnit;

        [XmlAttribute("angularUnit")]
        public string angularUnit;

        [XmlAttribute("directionUnit")]
        public string name;
    }

    [XmlRoot("Author")]
    public class Author
    {
        [XmlAttribute("createdBy")]
        public string createdBy;

        [XmlAttribute("createdByEmail")]
        public string createdByEmail;

        [XmlAttribute("company")]
        public string company;

        [XmlAttribute("companyURL")]
        public string companyURL;

    }
    [XmlRoot("Alignments")]
    public class Alignments
    {
        [XmlAttribute("desc")]
        public string desc;

        [XmlElement("Alignment")]
        public Alignment alignment { get; set; } 

    }

    [XmlRoot("Alignment")]
    public class Alignment
    {
        [XmlAttribute("name")]
        public string name;

        [XmlAttribute("desc")]
        public string desc;

        [XmlAttribute("length")]
        public string length;

        [XmlAttribute("staStart")]
        public string staStart;

        [XmlElement("AlignPIs")]
        public AlignPIs alignPIs { get; set; } 
    }
    [XmlRoot("AlignPIs")]
    public class AlignPIs
    {
        [XmlElement("AlignPI")]
        public List<AlignPI> alignPI { get; set; } 
    }

    [XmlRoot("AlignPI")]
    public class AlignPI
    {
        [XmlElement("PI")]
        public PI pi { get; set; } 

        [XmlElement("InSpiral")]
        public InSpiral inSpiral { get; set; } 

        [XmlElement("Curve1")]
        public Curve1 cureve1 { get; set; } 

        [XmlElement("OutSpiral")]
        public OutSpiral outSpiral { get; set; } 

        [XmlElement("Station")]
        public Station station { get; set; } 
    }

    [XmlRoot("Station")]
    public class Station
    {
        [XmlText]
        public string value { get; set; }
    }


    [XmlRoot("PI")]
    public class PI
    {
        [XmlAttribute("code")]
        public int code;

        [XmlAttribute("name")]
        public int name;

        [XmlText]
        public string value;
    }

    [XmlRoot("InSpiral")]
    public class InSpiral
    {
        [XmlElement("Spiral")]
        public Spiral spiral { get; set; } 

    }

    [XmlRoot("Spiral")]
    public class Spiral
    {
        [XmlAttribute("length")]
        public double length;

        [XmlAttribute("radiusEnd")]
        public double radiusEnd;

        [XmlAttribute("radiusStart")]
        public double radiusStart;

        [XmlAttribute("rot")]
        public string rot;

        [XmlAttribute("spiType")]
        public string spiType;
    }

    [XmlRoot("Curve1")]
    public class Curve1
    {
        [XmlElement("Curve")]
        public Curve curve { get; set; } 
    }

    [XmlRoot("Curve")]
    public class Curve
    {
        [XmlAttribute("rot")]
        public string rot;

        [XmlAttribute("radius")]
        public double radius;
    }
    [XmlRoot("OutSpiral")]
    public class OutSpiral
    {
        [XmlElement("Spiral")]
        public Spiral spiral { get; set; } 
    }
}