I've created classes from XSD Schema using xsd.exe (also tried with xsd2code which had better results in a way that they worked immediately, and with xsd.exe I have to debug some errors). XSD Schema I've used can be found at http://www.landxml.org/schema/LandXML-1.2/LandXML-1.2.xsd , and sample file can be found at http://landxml.org/schema/LandXML-1.1/samples/AASHTO%20SDMS/MntnRoad.xml .
My code for deserialization looks like:
var mySerializer = new XmlSerializer(typeof (LandXML), new XmlRootAttribute(""));
TextReader myFileStream = new StreamReader("myFile.xml");
var myObject = (LandXML) mySerializer.Deserialize(myFileStream);
My problem is that result of deserialization is list of items of type XmlElement, so if I try to access their properties, I can't easy do that. If I want to access, for example, some Alignment object attribute in myFile.xml, the code is similar to this:
var a = myObject.Items[5];
var b = (XmlElement) a;
var c = b.ChildNodes.Item(5).ChildNodes.Item(0).ChildNodes.Item(0).Attributes[0].Value;
It is obvious that this is not a way which is meant to be while deserializing XML to classes. My idea was like (for same element):
var c = LandXML.Alignments.Alignment.CoordGeometry.Curve.rot
I don't know what I'm doing wrong, I've tried with simpler schemas, and this code was working well. Please help and tnx in advance!
EDIT 1
this is at top of my class and I think that this List type generating troubles. And there is a more similar code in my generated classes
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;
}
}
Try this