2012-10-24 4 views
1

특정 방식으로 직렬화하려고하는 xsd가 있습니다. 다음과 같이 원하는 것을 얻을 수는 있지만 문제는 xsd2code가 아무 곳에서도 완전히 사용되지 않는 추가 클래스를 생성한다는 것입니다. 내가 잘못하고 있니? 또 다른 속임수가 있습니까?추가 클래스를 만들지 않고 xsd2code에서 XmlArrayAttribute 및 XmlArrayItemAttribute를 만드는 방법

<xsd:schema 
xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
elementFormDefault="qualified" > 

    <xsd:element name="UITranslatorConfiguration" > 
     <xsd:complexType> 
      <xsd:sequence> 
       <xsd:element ref="Queries" minOccurs="0" /> 
      </xsd:sequence> 
     </xsd:complexType> 
    </xsd:element> 

    <xsd:element name="Queries"> 
     <xsd:complexType> 
      <xsd:sequence> 
       <xsd:element ref="Query" minOccurs="0" maxOccurs="unbounded"/> 
      </xsd:sequence> 
     </xsd:complexType> 
    </xsd:element> 

    <xsd:element name="Query"> 
     <xsd:complexType> 
      <xsd:simpleContent> 
       <xsd:extension base="xsd:string"> 
        <xsd:attribute name="QueryID" type="xsd:string" /> 
       </xsd:extension> 
      </xsd:simpleContent> 
     </xsd:complexType> 
    </xsd:element> 

</xsd:schema> 

XML 출력 I 원하는 :

<UITranslatorConfiguration> 
    <Queries> 
     <Query QueryID="queryID1">someQueryText</Query> 
     <Query QueryID="queryiq2">someQueryText2</Query> 
     <Query QueryID="queryiq3">someQueryText3</Query> 
    </Queries> 
<UITranslatorConfiguration> 

가 생성하는 코드 :

이 미세이다

[System.CodeDom.Compiler.GeneratedCodeAttribute("Xsd2Code", "3.4.0.38968")] 
[System.SerializableAttribute()] 
[System.ComponentModel.DesignerCategoryAttribute("code")] 
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)] 
[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)] 
public partial class UITranslatorConfiguration { 

    [EditorBrowsable(EditorBrowsableState.Never)] 
    private List<Query> queriesField; 

    private static System.Xml.Serialization.XmlSerializer serializer; 

    public UITranslatorConfiguration() { 
     this.queriesField = new List<Query>(); 
    } 

    [System.Xml.Serialization.XmlArrayAttribute(Order=0)] 
    [System.Xml.Serialization.XmlArrayItemAttribute("Query", IsNullable=false)] 
    public List<Query> Queries { 
     get { 
      return this.queriesField; 
     } 
     set { 
      this.queriesField = value; 
     } 
    } 
} 

이 미세이다

[System.CodeDom.Compiler.GeneratedCodeAttribute("Xsd2Code", "3.4.0.38968")] 
[System.SerializableAttribute()] 
[System.ComponentModel.DesignerCategoryAttribute("code")] 
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)] 
[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)] 
public partial class Query { 

    [EditorBrowsable(EditorBrowsableState.Never)] 
    private string queryIDField; 

    [EditorBrowsable(EditorBrowsableState.Never)] 
    private string valueField; 

    private static System.Xml.Serialization.XmlSerializer serializer; 

    [System.Xml.Serialization.XmlAttributeAttribute()] 
    public string QueryID { 
     get { 
      return this.queryIDField; 
     } 
     set { 
      this.queryIDField = value; 
     } 
    } 

    [System.Xml.Serialization.XmlTextAttribute()] 
    public string Value { 
     get { 
      return this.valueField; 
     } 
     set { 
      this.valueField = value; 
     } 
    } 
} 

이것은 좋지 않습니다. 이것은 왜 그리고 왜 왔습니까? 그것은 전혀 사용되지 않습니다. xsd2code가이 클래스를 생성하지 않게하려면 어떻게해야합니까?

[System.CodeDom.Compiler.GeneratedCodeAttribute("Xsd2Code", "3.4.0.38968")] 
[System.SerializableAttribute()] 
[System.ComponentModel.DesignerCategoryAttribute("code")] 
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)] 
[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)] 
public partial class Queries { 

    [EditorBrowsable(EditorBrowsableState.Never)] 
    private List<Query> queryField; 

    private static System.Xml.Serialization.XmlSerializer serializer; 

    public Queries() { 
     this.queryField = new List<Query>(); 
    } 

    [System.Xml.Serialization.XmlElementAttribute("Query", Order=0)] 
    public List<Query> Query { 
     get { 
      return this.queryField; 
     } 
     set { 
      this.queryField = value; 
     } 
    } 
} 
+0

해결 되었습니까? – Mightymuke

+0

@Mightymuke 나는하지 않았다. 개체 모델에 여분의 클래스를 남겨 두었습니다. 그곳에 들어가서 아무 것도 아프지 않습니다. 사용하지 않는 코드는 파일을 압축하고 최소한의 코드에 대한 나의 욕구에 어긋나지 만 살 수 있습니다. – EbbnFlow

+1

나는 생성 된 코드 (예 : 필드 이름 등)와 비슷한 "문제"를 가졌습니다. 좋고 깨끗한 엔티티 클래스를 작성하고 [AutoMapper] (https://github.com/AutoMapper/AutoMapper)를 사용하여 데이터를 복사했습니다. 즉, 생성 된 클래스를 처리 할 필요가 없었으며 또한 [반부패 계층] (http : //www.markhneedham.com/blog/2009/07/07/도메인 기반 디자인 - 안티 - 부패 - 레이어 /). 그러나 xsd2code를 빠르게 살펴보고 재미있는 것을 찾으면 업데이트 할 것입니다. – Mightymuke

답변

0

나는 생성 된 코드 (예를 들어, 추한 필드 이름 등)와 유사한 "문제"를 가지고 있었다. 좋고 깨끗한 엔티티 클래스를 생성하고 생성 된 클래스의 데이터에서 초기화하기 위해 AutoMapper을 사용했습니다. 따라서 생성 된 클래스를 직접 처리 할 필요가 없었고 anti corruption layer도 제공했습니다. 생성 된 클래스가 스키마에서 예기치 않은 변경 사항으로부터 응용 프로그램을 보호하기 위해 세계에서 가장 아름다운 코드를 가지고 있더라도 이것이 내 추천 사항입니다.

그러나이 버전의 xsd2code (3.5.3에서는 여러 가지 문제를 해결하기 위해 추가 패치가있는 소스에서 직접 빌드 한 것입니다)에서이 문제를 테스트 한 결과이 동작이 여전히 발생하고 있음을 확인했습니다. xsd2code site에서 문제를 열 ​​것을 제안합니다.하지만 불행히도 문제를 해결하는 데 많은 초점을 두지 않는 것 같습니다. 아직 구현되지 않은 패치를 여러 개 제출 했으므로 곧 수정판을 보지 않을 것입니다. . 고맙게도 당신은 그 문제를 해결할 수있었습니다.

0

스키마에 명시적인 "쿼리"요소가 포함되어 있습니다. 이로 인해 클래스가 생성됩니다. "쿼리"클래스가없는 코드 세트를 얻으려면 쿼리 요소 유형을 "쿼리"유형으로 지정하면됩니다. 나는 사용을 방지하기 위해 더 발견 (도구에 관계없이) 자동 생성 된 클래스에 XSD 파일을 변환하여 내 경험에

namespace Schemas1 
{ 
    using System; 
    using System.Diagnostics; 
    using System.Xml.Serialization; 
    using System.Collections; 
    using System.Xml.Schema; 
    using System.ComponentModel; 
    using System.Collections.Generic; 


    public partial class UITranslatorConfiguration 
    { 

     private List<Query> queriesField; 

     public UITranslatorConfiguration() 
     { 
      this.queriesField = new List<Query>(); 
     } 

     public List<Query> Queries 
     { 
      get 
      { 
       return this.queriesField; 
      } 
      set 
      { 
       this.queriesField = value; 
      } 
     } 
    } 

    public partial class Query 
    { 

     private string queryIDField; 

     private string valueField; 

     public string QueryID 
     { 
      get 
      { 
       return this.queryIDField; 
      } 
      set 
      { 
       this.queryIDField = value; 
      } 
     } 

     [System.Xml.Serialization.XmlTextAttribute()] 
     public string Value 
     { 
      get 
      { 
       return this.valueField; 
      } 
      set 
      { 
       this.valueField = value; 
      } 
     } 
    } 
} 

을 같이

<?xml version="1.0" encoding="utf-8"?> 
<xs:schema id="XMLSchema1" 
    targetNamespace="http://tempuri.org/XMLSchema1.xsd" 
    elementFormDefault="qualified" 
    xmlns="http://tempuri.org/XMLSchema1.xsd" 
    xmlns:mstns="http://tempuri.org/XMLSchema1.xsd" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
> 

    <xs:element name="UITranslatorConfiguration"> 
    <xs:complexType> 
     <xs:sequence> 
     <xs:element name="Queries" type="Query" minOccurs="0" maxOccurs="unbounded"/> 
     </xs:sequence> 
    </xs:complexType> 
    </xs:element> 

    <xs:complexType name="Query"> 
    <xs:simpleContent> 
     <xs:extension base="xs:string"> 
     <xs:attribute name="QueryID" type="xs:string"/> 
     </xs:extension> 
    </xs:simpleContent> 
    </xs:complexType> 

</xs:schema> 

이 원하는 결과를 생성합니다 가능한 경우 "ref"를 사용하고 이러한 상황에서 직접 유형을 사용하십시오.

희망이 도움이됩니다.

+0

이렇게하면 xml 요소도 제거되고 Query 요소의 이름이 쿼리로 바뀝니다. 이것은 원하는 출력을 생성하지 않습니다. – Bart