Skip to main content
 首页 » 编程设计

c#之在 XML 反序列化中使用继承

2024年02月23日17jillzhang

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

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

请您参考如下方法:

我将从您尝试反序列化的 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; 
        } 
    } 
}