writing a Simplest XML DeSerialization class for the simplest xml file. How to avoid the nesting? de

Posted by Enggr on Stack Overflow See other posts from Stack Overflow or by Enggr
Published on 2010-05-12T16:14:56Z Indexed on 2010/05/12 16:24 UTC
Read the original article Hit count: 253

Filed under:
|
|
|
|

Hi,

I want to deserialize an xml file which has to be just in this form

<Basket>
  <Fruit>Apple</Fruit>
  <Fruit>Orange</Fruit>
  <Fruit>Grapes</Fruit>
</Basket>

Out of the examples I read on internet the least possible format I could find was the following

<Basket>
  <FruitArray>
    <Fruit>Apple</Fruit>
  </FruitArray>
  <FruitArray>
    <Fruit>Orange</Fruit>
  </FruitArray>
  <FruitArray>
    <Fruit>Grapes</Fruit>
  </FruitArray>
</Basket>

and that has the following deserialization class for converting it into a class object.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace XMLSerialization_Basket
{
    [System.Xml.Serialization.XmlRootAttribute("Basket", Namespace = "BasketNamespace", IsNullable = false)]
    public class Basket
    {
        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute("FruitArray")]
        public FruitArray[] objFruitArray;
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlTypeAttribute(Namespace = "BasketNamespace")]
    public class FruitArray
    {

        /// <remarks/>
        private string _Fruit;

        public string Fruit
        {

            get { return _Fruit; }
            set { _Fruit = value; }
        }
    }


}

Can I add something like the following directly under top class

    private string _Fruit;

    public string Fruit
    {

        get { return _Fruit; }
        set { _Fruit = value; }
    }

and avoid the array nesting?

my goal is to deserialize an xml of following format

<Basket>
  <Fruit>Apple</Fruit>
  <Fruit>Orange</Fruit>
  <Fruit>Grapes</Fruit>
</Basket>

© Stack Overflow or respective owner

Related posts about xml-deserialization

Related posts about Xml