2012-10-24 35 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輸出我想:

<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 /博客/ 2009/07/07 /領域驅動設計,反腐敗層/)。不過,如果我找到有趣的東西,我會快速瀏覽一下xsd2code並更新它。 – Mightymuke

回答

0

我有類似的「問題」與生成的代碼(例如,醜陋的字段名等)。我最終創建了很好且乾淨的實體類,並使用AutoMapper從生成的類中的數據初始化它們。這意味着我不必直接處理生成的類,它也提供了一個anti corruption layer。即使生成的類具有世界上最漂亮的代碼,這也是我的建議,只是爲了保護您的應用程序免受架構中的任何意外更改。但是,我剛剛在我的版本xsd2code(3.5.3直接從源代碼構建,並附帶一些補丁修復了我遇到的各種問題)中對此進行了測試,並且已確認此行爲仍在發生。我建議你在xsd2code site處打開一個問題,但不幸的是,似乎沒有太多的重點來解決它們(我已經提交了幾個尚未實現的補丁),所以我不會期望很快就會有修復。謝天謝地,你已經能夠解決它了。

0

您的模式包含顯式的「查詢」元素。這導致類的生成。要實現沒有「查詢」類的代碼集,只需指定類型爲「查詢」的查詢元素的類型即可。

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

這將產生如圖

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; 
      } 
     } 
    } 
} 

以我的經驗與XSD文件轉換爲自動生成的類(無論工具)我發現這是更好地避免使用需要的結果儘可能使用「ref」,而直接在這些情況下使用類型。

希望這會有所幫助。

+0

這也刪除了 xml元素,並將查詢元素重命名爲查詢。這不會產生所需的輸出。 – Bart