2011-12-17 2 views
1

상속을 사용하여 deserialize하려고합니다.XML deserialisation에서 상속 사용

<?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하지만 그들은 자신의 방식을 사용

예를 들어, 내가 곧 opensearch

위한 클래스가의 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/" /> 
    <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 객체를 deserialise하려고 할 때마다 OpenSearch 객체는 NULL입니다.

어떻게해야합니까? xsd.exe를 사용하여 코드를 생성하여 직접 예제를 만들려고했지만 SearchPlugin.xsd에 대한 코드를 생성하려고 할 때 오류가 계속 발생하므로 도움이되지 않았습니다.

답변

4

역 직렬화하려는 XML, 해당 XML에서 XSD 파일 가져 오기, 필요하다면 XSD를 조정하여 xsd.exe를 사용하여 클래스를 생성하십시오. 생성 된 클래스를 보면 누락 된 부분을 볼 수 있습니다. 정말 xsd.exe의 방법에 사용

누군가가 즉시 SearchPlugin 클래스의 오픈 서치 속성이

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

같은 더 XmlElementAttribute, 뭔가가 없음을 지적 할 나는 당신이 널 얻고있는 이유 믿습니다. 아래는 문제 해결 방법입니다 ...

XML로 시작하기. 의미

Mozilla SearchPlugin diagram

, 당신이 XSD를 사용할 수 있습니다 다음 XSD 다이어그램이 싶습니다

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

: 당신의 스키마와 클래스를 기반으로, 나는 당신이 뭔가 아래와 같은 기대한다고 가정 할 :

<?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입니다.

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; 
     } 
    } 
} 
당신은 xsd.exe를 실행하면

,이 출력을 얻을 수 있습니다