I think this should do want you want. I have to say that I didnt' use XSD to create my classes - I have created them from scratch.
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;
}
}
This would then create the actual XML file - which is almost the same as your example. The only difference being is that I think you will need to have an attribute in the Item element to hold the "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;
}
}
Let me know how you get on. I hope this is what you are looking for.
thanks
Barry