我想这应该千方百计想让你想要的。我不得不说,我没有'使用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;
}
}
让我知道你上车。我希望这是你正在寻找的。
感谢
巴里
我的问题出在哪里有点不清楚(即你在哪里卡住),相当你是什么意思与“一个接一个” /“无法建造它”等你能澄清你的意思吗? – 2010-03-30 11:22:59