【问题标题】:Use inheritance in XML deserialisation在 XML 反序列化中使用继承
【发布时间】:2011-12-17 23:16:31
【问题描述】:

我正在尝试使用继承进行反序列化。

例如,我有一个opensearch的类

简而言之,XSD 是这样的:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://a9.com/-/spec/opensearch/1.1/" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="OpenSearchDescription">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="ShortName" type="xs:string" />
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

Mozilla impements this 但他们使用自己的方案。

所以他们有:

<?xml version="1.0" encoding="Windows-1252"?>
<xs:schema xmlns:os="http://a9.com/-/spec/opensearch/1.1/" attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://www.mozilla.org/2006/browser/search/" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:import namespace="http://a9.com/-/spec/opensearch/1.1/" />
  <xs:element name="SearchPlugin">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="os:ShortName" />
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

对于 opensearch,我有一个类似

的类
[SerializableAttribute]
[XmlRoot(Namespace = "http://a9.com/-/spec/opensearch/1.1/", IsNullable = false, ElementName = "OpenSearchDescription")]
public class OpenSearch
{
    public string ShortName { get; set; }
}

当我尝试反序列化 xml 时,这很好用...

对于我的 mozilla 实现,我只想要这样的东西:

[System.SerializableAttribute]
[XmlRoot(Namespace = "http://www.mozilla.org/2006/browser/search/", IsNullable = false, ElementName = "SearchPlugin")]
public class SearchPlugin
{
    public OpenSearch OpenSearch { get; set; }
}

但是,每当我尝试反序列化 SearchPlugin 对象时,OpenSearch 对象只是 NULL。

我应该怎么做?我尝试通过使用 xsd.exe 生成代码来为自己创建一个示例,但是当我尝试为 SearchPlugin.xsd 生成代码时,我也不断收到错误,所以这没有帮助...

【问题讨论】:

    标签: c# xml serialization xsd


    【解决方案1】:

    我将从您尝试反序列化的 XML 开始,从该 XML 中获取 XSD 文件,如果需要,调整 XSD 以确保它们有效,然后使用 xsd.exe 生成类。通过查看生成的类,您会发现缺少什么。

    真正习惯 xsd.exe 方式的人会立即指出 SearchPlugin 类的 OpenSearch 属性没有 XmlElementAttribute,类似于

    [System.Xml.Serialization.XmlElementAttribute(Namespace="http://a9.com/-/spec/opensearch/1.1/")]
    

    我相信这就是为什么你会得到 null 的原因。以下是我将如何进行故障排除...

    从 XML 开始;根据您的架构和类,我会假设您期望以下内容:

    <?xml version="1.0" encoding="utf-8" standalone="yes"?>
    <!-- Sample XML generated by QTAssistant (http://www.paschidev.com) -->
    <SearchPlugin xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:os="http://a9.com/-/spec/opensearch/1.1/" xmlns="http://www.mozilla.org/2006/browser/search/">
        <os:OpenSearchDescription>
            <os:ShortName>ShortName1</os:ShortName>
        </os:OpenSearchDescription>
    </SearchPlugin>
    

    XSD 图是这样的:

    也就是说,您可以使用这些 XSD:

    <?xml version="1.0" encoding="Windows-1252"?>
    <xs:schema xmlns:os="http://a9.com/-/spec/opensearch/1.1/" attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://www.mozilla.org/2006/browser/search/" xmlns:xs="http://www.w3.org/2001/XMLSchema">
        <xs:import namespace="http://a9.com/-/spec/opensearch/1.1/" schemaLocation="OpenSearch.xsd"/>
        <xs:element name="SearchPlugin">
            <xs:complexType>
                <xs:sequence>
                    <xs:element ref="os:OpenSearchDescription"/>
                </xs:sequence>
            </xs:complexType>
        </xs:element>
    </xs:schema>
    

    OpenSearch.xsd 是您发布的那个。

    如果你运行 xsd.exe,你会得到这个输出:

    C:\.....>xsd MozillaOpenSearch.xsd OpenSearch.xsd /classes
    Microsoft (R) Xml Schemas/DataTypes support utility
    [Microsoft (R) .NET Framework, Version 4.0.30319.1]
    Copyright (C) Microsoft Corporation. All rights reserved.
    Writing file 'C:\....\OpenSearch.cs'.
    

    生成的类:

    using System.Xml.Serialization;
    
    // 
    // This source code was auto-generated by xsd, Version=4.0.30319.1.
    // 
    
    
    /// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true,     Namespace="http://www.mozilla.org/2006/browser/search/")]
    [System.Xml.Serialization.XmlRootAttribute(Namespace="http://www.mozilla.org/2006/browser/search/", IsNullable=false)]
    public partial class SearchPlugin {
    
        private OpenSearchDescription openSearchDescriptionField;
    
        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Namespace="http://a9.com/-/spec/opensearch/1.1/")]
        public OpenSearchDescription OpenSearchDescription {
            get {
                return this.openSearchDescriptionField;
            }
            set {
                this.openSearchDescriptionField = value;
            }
        }
    }
    
    /// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://a9.com/-/spec/opensearch/1.1/")]
    [System.Xml.Serialization.XmlRootAttribute(Namespace="http://a9.com/-/spec/opensearch/1.1/", IsNullable=false)]
    public partial class OpenSearchDescription {
    
        private string shortNameField;
    
        /// <remarks/>
        public string ShortName {
            get {
                return this.shortNameField;
            }
            set {
                this.shortNameField = value;
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-09-13
      • 2010-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-06
      相关资源
      最近更新 更多