2011-12-17 13 views
1

私は継承でデシリアライズしようとしています。XMLデシリアライゼーションの継承を使用

例えば、私はまもなく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が、彼らは自分自身のスキームを使用しています。私のmozillaの実装についてはOpenSearchのために

<?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> 

を私はXMLをdeserialiseしようとすると、これが正常に動作します

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

のようなクラスを持っている...

ので、彼らが持っています私はちょうどこのようなものが欲しい:

[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のコードを生成しようとするとエラーが発生するので、助けにはなりません...

答えて

4

私はデシリアライズしようとしているXML、そのXMLからXSDファイルを取得し、必要に応じてXSDを調整して有効であることを確認してから、xsd.exeを使用してクラスを生成します。生成されたクラスを見ることで、欠落しているものが表示されます。本当にXsd.exeではの方法に使用

誰かがすぐに私はそれはあなたがnullを取得している理由だと信じ、SearchPluginクラスのOpenSearchのプロパティは何のXmlElementAttributeを持っていないことを

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

のようなものを指摘します。以下は私がトラブルシューティングを行う方法です。

XMLで始まります。意味、あなたはこれらのXSDを使用することができます

Mozilla SearchPlugin diagram

<?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図はこのことを希望:あなたのスキーマとクラスに基づいて、私はあなたが以下のようなものを期待していることを前提としています:

<?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; 
     } 
    } 
} 
関連する問題