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;
}
}
TNX的幫助,但是,這並不工作 - 其實它的工作,但我生成的類文件是超過30K代碼行,這是手動更改所有這些的巨大工作。這裏的主要問題是,我有列表
我添加編輯1更好地描述問題 –
xml文件只能有一個根標籤。您在根級別有一個數組。更改一致性級別可能有效。從來沒有用它去反序列化。字符串FILENAME =「」; XmlReaderSettings settings = new XmlReaderSettings(); settings.ConformanceLevel = ConformanceLevel.Fragment; XmlReader reader = XmlTextReader.Create(FILENAME,settings); – jdweng