2011-10-20 46 views
1

我可能會做一些完全錯誤的,但我創建了一個簡單的測試模式:Xsd2Code幫助 - 生成的代碼似乎並不匹配模式

<?xml version="1.0" encoding="UTF-8"?> 
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified"> 
<xs:element name="MyRoot"> 
    <xs:annotation> 
     <xs:documentation>Comment describing your root element</xs:documentation> 
    </xs:annotation> 
    <xs:complexType> 
     <xs:choice> 
      <xs:element name="MyChildOne" minOccurs="0" maxOccurs="unbounded"> 
       <xs:complexType> 
        <xs:choice> 
         <xs:element name="SubChild" minOccurs="0" maxOccurs="unbounded"/> 
        </xs:choice> 
        <xs:attribute name="SomeAttribute" type="xs:string"/> 
        <xs:attribute name="SomethingElse" type="xs:string"/> 
       </xs:complexType> 
      </xs:element> 
      <xs:element name="MyChildTwo" type="xs:string" maxOccurs="unbounded"/> 
     </xs:choice> 
    </xs:complexType> 
</xs:element> 

一個根,兩個孩子(一個可選) 。

我從VS2010運行了Xsd2Code,生成的代碼創建了兩個「根」類(MyRoot和MyChildOne),而不創建預期的MyChildTwo。我本來期望與MyRoot.MyChildOne模型...

下面是生成的代碼:

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 MyRoot 
{ 

    private List<object> itemsField; 

    public MyRoot() 
    { 
     this.itemsField = new List<object>(); 
    } 

    public List<object> Items 
    { 
     get 
     { 
      return this.itemsField; 
     } 
     set 
     { 
      this.itemsField = value; 
     } 
    } 
} 

public partial class MyRootMyChildOne 
{ 

    private List<object> itemsField; 

    private string someAttributeField; 

    private string somethingElseField; 

    public MyRootMyChildOne() 
    { 
     this.itemsField = new List<object>(); 
    } 

    public List<object> Items 
    { 
     get 
     { 
      return this.itemsField; 
     } 
     set 
     { 
      this.itemsField = value; 
     } 
    } 

    public string SomeAttribute 
    { 
     get 
     { 
      return this.someAttributeField; 
     } 
     set 
     { 
      this.someAttributeField = value; 
     } 
    } 

    public string SomethingElse 
    { 
     get 
     { 
      return this.somethingElseField; 
     } 
     set 
     { 
      this.somethingElseField = value; 
     } 
    } 
} 

我不明白我怎麼能序列化爲有效的(架構兼容)的XML文件的.. 。爲培養我對這個

的Cos

+0

三個問題,沒有標記爲回答。 –

回答

1

如果使用XSD.EXE生成該類

謝謝,它給你同樣的事情:

public partial class MyRoot { 

    private object[] itemsField; 

    /// <remarks/> 
    [System.Xml.Serialization.XmlElementAttribute("MyChildOne", typeof(MyRootMyChildOne))] 
    [System.Xml.Serialization.XmlElementAttribute("MyChildTwo", typeof(string))] 
    public object[] Items { 
     get { 
      return this.itemsField; 
     } 
     set { 
      this.itemsField = value; 
     } 
    } 
} 

除使用已知類型聲明:

[System.Xml.Serialization.XmlElementAttribute("MyChildTwo", typeof(string))] 

因此,它是有道理的,如果你想想看。因爲您的孩子類型2是一個字符串,並且字符串是XSD中的簡單類型,所以您可以將System.String的實例添加到您的Items數組中,然後使用上面的代碼將其序列化。每個字符串將被包裝在一個<MyChildTwo/>節點中。

UPDATE

爲了使這項工作,你創建你的類型,然後使用XmlSerializer的:

var root = new MyRoot(); 
root.Items = new object[2]; 
root.Items[0] = new MyRootMyChildOne { Items = new object[1], SomeAttribute = "test", SomethingElse = "test" }; 
root.Items[1] = "hello"; 

var ser = new XmlSerializer(typeof(MyRoot)); 
var memoryStream = new MemoryStream(); 
var xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8); 
var streamReader = new StreamReader(memoryStream, Encoding.UTF8); 
ser.Serialize(xmlTextWriter, root); 
memoryStream.Position = 0; 

string xml = streamReader.ReadToEnd(); 

這給了我們以下XML:

<MyRoot xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <MyChildOne SomeAttribute="test" SomethingElse="test" /> 
    <MyChildTwo>hello</MyChildTwo> 
</MyRoot> 
+0

謝謝,但我不明白,我跑這個代碼(生成的代碼後): 'MyRoot rt = new MyRoot(); rt.Items.Add(「sample string」); MyRootMyChildOne child = new MyRootMyChildOne(); rt.SaveToFile(「samplefile.xml」);' 但生成的XML是: –

+0

'<?xml version =「1.0」encoding =「utf-8」?> sample串 ' –

+0

,我仍然不知道如何MyChildOne結構中添加與生成的代碼混合......再次感謝 –