2016-03-01 52 views
0

我使用XSD.EXE創建的類從XSD架構(也試圖與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中一些調整對象的屬性,代碼類似這樣:

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

這是班裏的第一名,我認爲這個列表類型發生的麻煩。並且在我的生成類

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; 
      } 
     } 

回答

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; } 
    } 
} 
+0

TNX的幫助,但是,這並不工作 - 其實它的工作,但我生成的類文件是超過30K代碼行,這是手動更改所有這些的巨大工作。這裏的主要問題是,我有列表其中存儲其他對象(如對齊),我不能直接訪問它們,因爲我可以如果我改變結構看起來像你的 –

+0

我添加編輯1更好地描述問題 –

+0

xml文件只能有一個根標籤。您在根級別有一個數組。更改一致性級別可能有效。從來沒有用它去反序列化。字符串FILENAME =「」; XmlReaderSettings settings = new XmlReaderSettings(); settings.ConformanceLevel = ConformanceLevel.Fragment; XmlReader reader = XmlTextReader.Create(FILENAME,settings); – jdweng