奇怪的反序列化问题 [英] Strange deserialization issue
问题描述
我已经使用 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.
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);
我的问题是反序列化的结果是 XmlElement 类型的项目列表,所以如果我尝试访问它们的属性,我就不容易做到这一点.例如,如果我想访问 myFile.xml 中的某些 Alignment 对象属性,则代码类似于:
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;
很明显,这不是将 XML 反序列化为类的方式.我的想法就像(对于相同的元素):
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
我不知道我做错了什么,我尝试过使用更简单的模式,并且这段代码运行良好.请提前帮助和tnx!
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!
编辑 1
这是我班上的佼佼者,我认为这种 List 类型会产生麻烦.并且在我生成的类中有更相似的代码
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;
}
}
推荐答案
试试这个
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; }
}
}
这篇关于奇怪的反序列化问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!