在C#中序列化类。嵌套的XML

时间:2010-03-30 09:27:19

标签: c# xml serialization

我必须以下面的格式生成XML

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

所以我通过将上面的XML转换为XSD,使用xsd.exe生成了CS文件。

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

生成的CS文件包含4个类。

我的问题是我必须使用生成的类以上述格式构建XML。我能够逐个序列化类文件,一次重新生成一个标签,但我无法按照上面提到的方式构建它。

请帮忙

Regardas, jebli

1 个答案:

答案 0 :(得分: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;

            }



        }

让我知道你是怎么过的。我希望这就是你要找的东西。

感谢

百里