2

I have to generate XML in the below format

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

So I have generated the CS file using xsd.exe by converting the above XML to XSD.

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

The CS file that is generated contains 4 classes.

My question is i have to build the XML in the above mentioned format using the class that is generated. I am able to serialize the class files one by one which retirns one tag at a time, but i am unable to build it in the way that i mentioned above.

Please help

Regardas, jebli

1
  • I'm a little unclear on where the problem is (i.e. where you are stuck) and quite what you mean with the "one by one" / "unable to built it" etc. Can you clarify what you mean? Commented Mar 30, 2010 at 11:22

1 Answer 1

4

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.