一个相当复杂的问题,所以提前谢谢。以下两个 xml 文件都针对给定的模式进行验证,但在尝试使用 .Net 的 XmlSerializer 进行反序列化时,只有第一个正确:
<ex:iso_10303_28 xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.iai-tech.org/ifcXML/IFC2x3/FINAL" version="2.0" xmlns:ex="urn:iso.org:standard:10303:part(28):version(2):xmlschema:common">
<ex:iso_10303_28_header>
<ex:name>An Example</ex:name>
<ex:time_stamp>2010-11-12T13:04:00</ex:time_stamp>
<ex:author>John Hancock</ex:author>
<ex:organization>MegaCorp</ex:organization>
<ex:preprocessor_version>a preprocessor</ex:preprocessor_version>
<ex:originating_system>IfcXml dotNet Library</ex:originating_system>
<ex:authorization>none</ex:authorization>
<ex:documentation>documentation</ex:documentation>
</ex:iso_10303_28_header>
<ex:uos xsi:type="uos" id="uos_1" configuration="i-ifc2x3">
<ex:Entity xsi:type="IfcOrganization" id="i1101">
<Id xsi:nil="true" />
<Name>MegaCorp</Name>
<Description xsi:nil="true" />
<Roles xsi:nil="true" />
<Addresses xsi:nil="true" />
</ex:Entity>
<ex:Entity xsi:type="IfcCartesianPoint" id="i101">
<Coordinates ex:itemType="ifc:IfcLengthMeasure" ex:cType="list">
<IfcLengthMeasure>2500</IfcLengthMeasure>
<IfcLengthMeasure>0</IfcLengthMeasure>
<IfcLengthMeasure>0</IfcLengthMeasure>
</Coordinates>
</ex:Entity>
<ex:Entity xsi:type="IfcDirection" id="i102">
<DirectionRatios ex:itemType="ex:double-wrapper" ex:cType="list">
<ex:double-wrapper>0</ex:double-wrapper>
<ex:double-wrapper>1</ex:double-wrapper>
<ex:double-wrapper>0</ex:double-wrapper>
</DirectionRatios>
</ex:Entity>
</ex:uos>
</ex:iso_10303_28>
但第二个文件没有正确反序列化,在下面(iso_10303_28.uos as uos1).Items
被反序列化为空:
<?xml version="1.0" encoding="UTF-8"?>
<ex:iso_10303_28
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ex=
"urn:iso.org:standard:10303:part(28):version(2):xmlschema:common"
xmlns="http://www.iai-tech.org/ifcXML/IFC2x3/FINAL"
xsi:schemaLocation="http://www.iai-tech.org/ifcXML/IFC2x3/FINAL
http://www.iai-tech.org/ifcXML/IFC2x3/FINAL/IFC2x3.xsd"
version="2.0">
<ex:iso_10303_28_header>
<ex:name>An Example</ex:name>
<ex:time_stamp>2010-11-12T13:04:00</ex:time_stamp>
<ex:author>John Hancock</ex:author>
<ex:organization>MegaCorp</ex:organization>
<ex:preprocessor_version>a preprocessor</ex:preprocessor_version>
<ex:originating_system>IfcXml dotNet Library</ex:originating_system>
<ex:authorization>none</ex:authorization>
<ex:documentation>documentation</ex:documentation>
</ex:iso_10303_28_header>
<uos id="uos_1" description="" configuration="i-ifc2x3" edo="">
<IfcOrganization id="i1101">
<Name>MegaCorp</Name>
</IfcOrganization>
<IfcCartesianPoint id="i101">
<Coordinates>
<IfcLengthMeasure>2500.0</IfcLengthMeasure>
<IfcLengthMeasure>0.0</IfcLengthMeasure>
<IfcLengthMeasure>0.0</IfcLengthMeasure>
</Coordinates>
</IfcCartesianPoint>
<IfcDirection id="i102">
<DirectionRatios>
<ex:double-wrapper>0.</ex:double-wrapper>
<ex:double-wrapper>1.</ex:double-wrapper>
<ex:double-wrapper>0.</ex:double-wrapper>
</DirectionRatios>
</IfcDirection>
</uos>
</ex:iso_10303_28>
我的反序列化器本质上是:
iso_10303_28 deserialized = (iso_10303_28)serializer.Deserialize( reader );
//using NUnit
Assert.IsNotNull(deserialized);
Assert.IsNotNull(deserialized.uos);
uos1 uos1 = deserialized.uos as uos1;
Assert.IsNotNull(uos1);
Assert.IsNotNull(uos1.Items); //<---FAILS HERE
Assert.AreEqual(3, uos1.Items.length);
uos 类是:
[System.Xml.Serialization.XmlTypeAttribute(TypeName="uos", Namespace="http://www.iai-tech.org/ifcXML/IFC2x3/FINAL")]
[System.Xml.Serialization.XmlRootAttribute("uos", Namespace="http://www.iai-tech.org/ifcXML/IFC2x3/FINAL", IsNullable=false)]
public partial class uos1 : uos {
private Entity[] itemsField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("Entity", Namespace="urn:iso.org:standard:10303:part(28):version(2):xmlschema:common")]
public Entity[] Items {
get {
return this.itemsField;
}
set {
this.itemsField = value;
}
}
}
[System.Xml.Serialization.XmlIncludeAttribute(typeof(uos1))]
[System.Xml.Serialization.XmlTypeAttribute( Namespace="urn:iso.org:standard:10303:part(28):version(2):xmlschema:common")]
public partial class uos {
private string idField;
private string[] expressField;
private string[] configurationField;
private string[] schemaLocationField;
private string edoField;
private string descriptionField;
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute(DataType="ID")]
public string id {
get {
return this.idField;
}
set {
this.idField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute(DataType="anyURI")]
public string[] express {
get {
return this.expressField;
}
set {
this.expressField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute(DataType="anyURI")]
public string[] configuration {
get {
return this.configurationField;
}
set {
this.configurationField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute(DataType="anyURI")]
public string[] schemaLocation {
get {
return this.schemaLocationField;
}
set {
this.schemaLocationField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute(DataType="anyURI")]
public string edo {
get {
return this.edoField;
}
set {
this.edoField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string description {
get {
return this.descriptionField;
}
set {
this.descriptionField = value;
}
}
}
类iso_10303_28
如下:
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="urn:iso.org:standard:10303:part(28):version(2):xmlschema:common")]
[System.Xml.Serialization.XmlRootAttribute(Namespace="urn:iso.org:standard:10303:part(28):version(2):xmlschema:common", IsNullable=false)]
public partial class iso_10303_28 {
private iso_10303_28_header iso_10303_28_headerField;
private uos uosField;
private string versionField;
/// <remarks/>
public iso_10303_28_header iso_10303_28_header {
get {
return this.iso_10303_28_headerField;
}
set {
this.iso_10303_28_headerField = value;
}
}
public enum uosChoice
{
[XmlEnum("http://www.iai-tech.org/ifcXML/IFC2x3/FINAL:uos")]
uos1,
[XmlEnum("urn:iso.org:standard:10303:part(28):version(2):xmlschema:common:uos")]
uos
}
[XmlIgnore()]
public uosChoice uosChoiceField;
[XmlChoiceIdentifier("uosChoiceField")]
[XmlElement(ElementName = "uos", Namespace = "http://www.iai-tech.org/ifcXML/IFC2x3/FINAL", Type = typeof(uos1))]
[XmlElement(ElementName = "uos", Namespace = "urn:iso.org:standard:10303:part(28):version(2):xmlschema:common")]
public uos uos {
get {
return this.uosField;
}
set {
this.uosField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string version {
get {
return this.versionField;
}
set {
this.versionField = value;
}
}
}
有没有人遇到过这个问题?是否有一个原因?我错过了什么吗?有修复或解决方法吗?
由于模式和其余代码的大小相当大,并且试图将它们简化到最小的失败部分会导致更多问题(请参阅这个问题)我没有在这里粘贴它们。但是,如果需要,可以在http://code.google.com/p/ifc-dotnet/找到此问题的架构、单元测试和来源