0

I have XML document:

<?xml version="1.0" encoding="utf-8" ?>
<Menu>
  <MainMenu>
    <Meal>Example1</Meal>
    <Meal>Example1</Meal>
    <Meal>Example1</Meal>
    <Meal>Example1</Meal>
    <Meal>Example1</Meal>
    <Meal>Example1</Meal>
  </MainMenu>
</Menu>

And I whant to deserialize it as list in class MainMenu:

   [Serializable()]
    public class MainMenu
    {
        [System.Xml.Serialization.XmlElementAttribute("Meal")]
        private List<string> Meal;

        public MainMenu()
        {
            Meal = new List<string>();
        }
    }

By method:

private void MenuDeserializer()
{
    MainMenu mainMenu = null;
    string path = "MenuXML.xml";

    XmlSerializer serializer = new XmlSerializer(typeof(MainMenu));

    StreamReader reader = new StreamReader(path);
    reader.ReadToEnd();
    mainMenu = (MainMenu)serializer.Deserialize(reader);
    reader.Close();
}

Is this will deserialize all Meal's to my list? and if not than how to do this? When I'm trying to debug I get exception: Error at XML file (0,0), this (0,0) is confusing, where is problem and how to solve it?

Carlos28
  • 2,381
  • 3
  • 21
  • 36
  • possible duplicate of [How to Deserialize XML document](http://stackoverflow.com/questions/364253/how-to-deserialize-xml-document) – Marius Bancila Apr 14 '14 at 10:56
  • I saw that artickle, but there is non about how to deserialize elements to list, and I'm using method from there but still i get error at XML file (0,0) – Carlos28 Apr 14 '14 at 10:59
  • you should not put XmlElementAttribute attribute for your Meal property in your MainMenu class, since Meal is not an attribute of MainMenu node in your XML – Savaratkar Apr 14 '14 at 11:01
  • Remove that attribute and try again... – Savaratkar Apr 14 '14 at 11:01
  • Damn your code is confusing now. by seeing your XML, you should have a class name 'Menu' with property of a class 'MainMenu'. The class 'MainMenu' should have list of 'Menu' property. – Savaratkar Apr 14 '14 at 11:03

3 Answers3

1

1 - Remove reader.ReadToEnd(), you're moving the stream from the start so when you come to deserialize it tries to start from the end of the file.

2 - You need to remove the outer Menu element from your XML, your class starts at MainMenu so so should your xml...

<?xml version="1.0" encoding="utf-8" ?>
<MainMenu>
    <Meal>Example1</Meal>
    <Meal>Example1</Meal>
    <Meal>Example1</Meal>
    <Meal>Example1</Meal>
    <Meal>Example1</Meal>
    <Meal>Example1</Meal>
</MainMenu>
DoctorMick
  • 6,703
  • 28
  • 26
0

If this is ur XML:

<?xml version="1.0" encoding="utf-8" ?>
<Menu>
  <MainMenu>
    <Meal>Example1</Meal>
    <Meal>Example1</Meal>
    <Meal>Example1</Meal>
    <Meal>Example1</Meal>
    <Meal>Example1</Meal>
    <Meal>Example1</Meal>
  </MainMenu>
</Menu>

And I whant to deserialize it as list in class MainMenu:

   public class Menu
   {
      public MainMenu MainMenu{get;set;}
   }
    public class MainMenu
    {

        public List<string> Meal {get; set;}

        public MainMenu()
        {
            Meal = new List<string>();
        }
    }

By method:

private void MenuDeserializer()
{
    Menu menu;
    string path = "MenuXML.xml";

    XmlSerializer serializer = new XmlSerializer(typeof(Menu));

    using(StreamReader reader = new StreamReader(path))
    {
        menu = (Menu)serializer.Deserialize(reader);
    }
}

This is how it shud be...

Savaratkar
  • 1,974
  • 1
  • 24
  • 44
0

The XML that you shown could be deserialized like this:

  [Serializable()]
  [System.Xml.Serialization.XmlRoot("Menu")]
  public class Menu
  {
     [XmlArray("MainMenu")]
     [XmlArrayItem("Meal", typeof(string))]
     public string[] MainMenu { get; set; }
  }

  public static void Main(string[] args)
  {
     XmlSerializer serializer = new XmlSerializer(typeof(Menu));

     using(StreamReader reader = new StreamReader("MenuXML.xml"))
     {
        Menu menu = (Menu)serializer.Deserialize(reader);
        reader.Close();
     }
  }
Marius Bancila
  • 16,053
  • 9
  • 49
  • 91