2010-03-30 37 views
2

我在下面格式在C#中序列化類。嵌套的XML

<objects> 
    <items> 
     <item ="delete"> 
      <searchfields> 
      <searchfield name="itemname" value="itemValue" /> 
      </searchfields> 
     </item> 
    </items> 
</objects> 

因此,我已生成使用XSD.EXE由上述XML轉換爲XSD的CS文件來生成XML。

xsd.exe -c -l:c# -n:XmlSerializationDeleteObject DeleteObject.xsd 

生成的CS文件包含4個類。

我的問題是我必須用上面提到的格式使用生成的類來構建XML。我能夠逐個序列化類文件,每次都會退回一個標籤,但我無法按照上面提到的方式構建它。

請幫

Regardas, jebli

+0

我的問題出在哪裏有點不清楚(即你在哪裏卡住),相當你是什麼意思與「一個接一個」 /「無法建造它」等你能澄清你的意思嗎? – 2010-03-30 11:22:59

回答

4

我想這應該千方百計想讓你想要的。我不得不說,我沒有'使用XSD創建我的類 - 我從頭開始創建它們。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Xml.Serialization; 
using System.Collections; 

namespace TestLibrary 
{ 
    [Serializable] 
    [XmlRoot("Objects")] 
    public class ObjectTest : ICollection 
    { 
     [XmlArray("Items")] 
     public Items[] items; 

     #region "Required for implementing ICollection" 
     //Default Accessor 
     public Items this[int index] 
     { 
      get { return (Items)items[index]; } 
     } 

     public void CopyTo(Array array, int index) 
     { 
      items.CopyTo(array, index); 
     } 

     public int Count 
     { 
      get { return items.Length; } 
     } 

     public bool IsSynchronized 
     { 
      get { return false; } 
     } 

     public object SyncRoot 
     { 
      get { return this; } 
     } 



     public IEnumerator GetEnumerator() 
     { 
      return items.GetEnumerator(); 
     } 

     public void Add(Items newItems) 
     { 
      if (this.items == null) 
      { 
       this.items = new Items[1]; 
      } 
      else 
      { 
       Array.Resize(ref this.items, this.items.Length + 1); 
      } 
      this.items[this.items.GetUpperBound(0)] = newItems; 

     } 
     #endregion 
    } 

    [Serializable] 
    public class Items 
    { 
     [XmlElement("Item")] 
     public Item item; 



    } 
    [Serializable] 
    public class Item 
    { 
     [XmlAttribute("itemType")] 
     public string itemType; 


     [XmlArray("SearchField")] 
     public SearchFields[] searchfields; 
    } 

    [Serializable] 
    public class SearchFields 
    { 

     [XmlAttribute("name")] 
     public string searchName; 

     [XmlAttribute("value")] 
     public string searchValue; 

    } 


} 

然後這將創建實際的XML文件 - 這與您的示例幾乎相同。唯一的區別是,我認爲你必須Item元素的屬性來保存"delete"

private void button1_Click(object sender, EventArgs e) 
     { 
      //Create the Serialize object to save the class to an XML file 
      XmlSerializer serializer = new XmlSerializer(typeof(ObjectTest)); 
      FileStream fs = new FileStream(@"C:\Objects.xml", FileMode.Create); 

      try 
      { 


       //Create new instances of each class to store the data 
       ObjectTest testing = new ObjectTest(); 
       Items newItems = new Items(); 
       Item newItem = new Item(); 
       SearchFields newSearch = new SearchFields(); 

       //Assign SearchField data 
       newSearch.searchName = "itemName"; 
       newSearch.searchValue = "itemValue"; 

       //Assign the item type 
       newItem.itemType = "delete"; 

       //Create a new array of SearchField objects 
       SearchFields[] testSearch = { newSearch }; 

       //Add the SearchField array to the Item class 
       newItem.searchfields = testSearch; 

       //Add the single Item class to the Items class 
       newItems.item = newItem; 

       //Create a new array of Items objects 
       Items[] testItems = { newItems }; 

       //Add the Items array to the ObjectTest class 
       testing.items = testItems; 

       //Serialize the object 
       serializer.Serialize(fs, testing); 


      } 
      catch (Exception ex) 
      { 
       MessageBox.Show("Error: " + ex.ToString()); 
      } 
      finally 
      { 
       //close the objects 
       fs.Close(); 
       serializer = null; 

      } 



     } 

讓我知道你上車。我希望這是你正在尋找的。

感謝

巴里

+0

嗨,謝謝你的答案,它幫助了我。 :) – Jebli 2010-04-16 12:09:09