首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用System.Xml.Serialization.XmlSerializer对有效xml文件进行反序列化时存在差异

使用System.Xml.Serialization.XmlSerializer对有效xml文件进行反序列化时存在差异
EN

Stack Overflow用户
提问于 2010-11-15 02:59:47
回答 1查看 1.5K关注 0票数 2

这是一个相当复杂的问题,所以提前谢谢你。以下两个xml文件都针对给定的模式进行验证,但在尝试使用.Net的XmlSerializer进行反序列化时,只有第一个文件可以正确执行:

代码语言:javascript
复制
<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中被反序列化为空:

代码语言:javascript
复制
<?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>

我的反序列化程序本质上是:

代码语言:javascript
复制
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类为:

代码语言:javascript
复制
[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的类如下所示:

代码语言:javascript
复制
[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;
        }
    }
}

以前有没有人遇到过这个问题?这是有原因的吗?我错过了什么吗?是否有修复或解决方法?

由于模式和其余代码的大小相当大,并且试图将它们简化到最小的失败部分,导致了更多的问题(请参阅this question),我没有在这里粘贴它们。但是,如果需要,可以在http://code.google.com/p/ifc-dotnet/上找到解决此问题的模式、单元测试和源代码

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-11-18 10:38:29

这两个XML文件是不同的,在file-1中,uso在ex下是限定的,因此它在"urn:iso.org:standard:10303:part(28):version(2):xmlschema:common“名称空间下,而在第二个文件中,它不是,而是在默认名称空间下。

在类' iso_10303_28‘属性uos类型的uos中,这意味着XmlSerializer将期望具有名为'uos’(默认情况下)的元素位于与iso_10303_28和'xsi: type‘'uos’相同的命名空间下,如类‘uos’所描述-在相同的- "urn:iso.org:standard:10303:part(28):version(2):xmlschema:common“命名空间下。这就解释了为什么在第二个xml中得到null。如果您无法控制xml,并且您应该能够反序列化它们中的任何一个,请尝试执行以下操作

除非由XmlInclude属性指定,否则

  1. 类型uos不知道类型uos1。我认为您在uos类上额外添加了一个XmlInclude属性,以将uos1作为已知类型包含在内。如果不是,序列化程序将不会反序列化第一个文件。如果没有-您应该这样做,如下所示

System.Xml.Serialization.XmlInclude(typeof(uos1))公共分部类uos {

}

  • 您可以为property iso_10303_28.uos添加XmlElement属性,以接受任一命名空间下的名称-并使用选项标识符或使用'uos[]‘而不是'uos’作为类型。使用选项标识符-它可以像

一样完成

System.Xml.Serialization.XmlRootAttribute(Namespace = "urn:iso.org:standard:10303:part(28):version(2):xmlschema:common",IsNullable = false)公共分部类iso_10303_28 {私有uos uosField;

代码语言:javascript
复制
    public enum uosChoice
    {
        [XmlEnum("http://www.iai-tech.org/ifcXML/IFC2x3/FINAL:uos")]
        uos,
        [XmlEnum("urn:iso.org:standard:10303:part(28):version(2):xmlschema:common:uos")]
        uos1
    }

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


}

EDIT:要正确解析项目,可以像这样修改uos1

代码语言:javascript
复制
 [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/>
        [XmlElement(ElementName = "Entity", Namespace = "urn:iso.org:standard:10303:part(28):version(2):xmlschema:common", Type = typeof(Entity))]
        [XmlElement(ElementName = "IfcOrganization", Namespace = "http://www.iai-tech.org/ifcXML/IFC2x3/FINAL", Type = typeof(IfcOrganization))]
        [XmlElement(ElementName = "IfcCartesianPoint", Namespace = "http://www.iai-tech.org/ifcXML/IFC2x3/FINAL", Type = typeof(IfcCartesianPoint))]
        [XmlElement(ElementName = "IfcDirection", Namespace = "http://www.iai-tech.org/ifcXML/IFC2x3/FINAL", Type = typeof(IfcDirection))]
        public Entity[] Items
        {
            get
            {
                return this.itemsField;
            }
            set
            {
                this.itemsField = value;
            }
        }

    }

 [System.Xml.Serialization.XmlTypeAttribute(TypeName = "IfcOrganization", Namespace = "http://www.iai-tech.org/ifcXML/IFC2x3/FINAL")]
    public class IfcOrganization : Entity
    {

    }

    [System.Xml.Serialization.XmlTypeAttribute(TypeName = "IfcCartesianPoint", Namespace = "http://www.iai-tech.org/ifcXML/IFC2x3/FINAL")]
    public class IfcCartesianPoint : Entity
    {

    }

    [System.Xml.Serialization.XmlTypeAttribute(TypeName = "IfcDirection", Namespace = "http://www.iai-tech.org/ifcXML/IFC2x3/FINAL")]
    public class IfcDirection : Entity
    {

    }

    [XmlInclude(typeof(IfcOrganization))]
    [XmlInclude(typeof(IfcCartesianPoint))]
    [XmlInclude(typeof(IfcDirection))]
    [System.Xml.Serialization.XmlTypeAttribute(TypeName = "Entity", Namespace = "urn:iso.org:standard:10303:part(28):version(2):xmlschema:common")]
    public class Entity
    {

    }
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4179086

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档