Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deserialization of xml file by using XmlArray?

I am trying to deserialize this xml structure.

<?xml version="1.0"?>
<DietPlan>
    <Health>
        <Fruit>Test</Fruit>
        <Fruit>Test</Fruit>
        <Veggie>Test</Veggie>
        <Veggie>Test</Veggie>
    </Health>
</DietPlan>

And I tried:

[Serializable]
[XmlRoot(ElementName = "DietPlan")]
public class TestSerialization
{
    [XmlArray("Health")]
    [XmlArrayItem("Fruit")]
    public string[] Fruits { get; set; }

    [XmlArray("Health")]
    [XmlArrayItem("Veggie")]
    public string[] Veggie { get; set; }
}

But this throws an exception "The XML element is already present in the current scope. Use XML attributes to specify another XML name or namespace for the element." Thanks in adv.

like image 660
now he who must not be named. Avatar asked Apr 09 '13 16:04

now he who must not be named.


People also ask

What is Xmlarray?

XML Array has defined as a variable array grouping together the same items in the list and contains one or more child items. Arrays being a sequence of elements declared with the same name. A multi-dimensional Array is created for a collection of elements. The arrays are done by creating functions by pairs.

What is Deserialization XML?

Serialization is a process by which an object's state is transformed in some serial data format, such as XML or binary format. Deserialization, on the other hand, is used to convert the byte of data, such as XML or binary data, to object type.


1 Answers

You need a common type to be able to deserialize your XML, and with that you can define with the [XmlElement] namespace what type to instantiate depending on the name of the element, as shown below.

public class StackOverflow_15907357
{
    const string XML = @"<?xml version=""1.0""?>
                        <DietPlan>
                            <Health>
                                <Fruit>Test</Fruit>
                                <Fruit>Test</Fruit>
                                <Veggie>Test</Veggie>
                                <Veggie>Test</Veggie>
                            </Health>
                        </DietPlan>";

    [XmlRoot(ElementName = "DietPlan")]
    public class TestSerialization
    {
        [XmlArray("Health")]
        [XmlArrayItem("Fruit", Type = typeof(Fruit))]
        [XmlArrayItem("Veggie", Type = typeof(Veggie))]
        public Food[] Foods { get; set; }
    }

    [XmlInclude(typeof(Fruit))]
    [XmlInclude(typeof(Veggie))]
    public class Food
    {
        [XmlText]
        public string Text { get; set; }
    }

    public class Fruit : Food { }
    public class Veggie : Food { }

    public static void Test()
    {
        MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(XML));
        XmlSerializer xs = new XmlSerializer(typeof(TestSerialization));
        TestSerialization obj = (TestSerialization)xs.Deserialize(ms);
        foreach (var food in obj.Foods)
        {
            Console.WriteLine("{0}: {1}", food.GetType().Name, food.Text);
        }
    }
}
like image 175
carlosfigueira Avatar answered Oct 09 '22 06:10

carlosfigueira