在XML反序列化中使用继承

时间:2011-12-17 23:16:32

标签: c# xml serialization xsd

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

例如,我有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生成代码时,我也不断收到错误,所以这没有帮助......

1 个答案:

答案 0 :(得分:4)

我将从您尝试反序列化的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图表是这样的:

Mozilla SearchPlugin diagram

这意味着,您可以使用这些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;
        }
    }
}