1

I have this class:

public class MyMenu
{
    public string Name { get; set; }
    public string Type { get; set; }
}

This class I want to use it in a dynamic menu, and I do not want to store his data in a database.

I want to store its data in Xml file.

Till now I have this for saving data:

string path = Server.MapPath("~/Content/Files");
XmlSerializer serial = new XmlSerializer(model.GetType());
System.IO.StreamWriter writer = new System.IO.StreamWriter(path + "\\ribbonmenu");
serial.Serialize(writer, model);
writer.Close();

And this to get the data:

string path = Server.MapPath("~/Content/Files");
XmlSerializer serial = new XmlSerializer(typeof(RibbonMenu));
System.IO.StreamReader reader = new System.IO.StreamReader(path + "\\ribbonmenu");          
RibbonMenu menu =(RibbonMenu) serial.Deserialize(reader);
reader.Close();

What I have is working for one object to save and get. I need to save multiple objects, and get the collection of objects, something like:

IEnumerable<MyMenu> model=(IEnumerable<MyMenu>) serial.Deserialize(reader);

Can someone give me a solution? Thanks.

Edit: The content of the generated Xml with my code is:

<?xml version="1.0" encoding="utf-8"?>
<MyMenu xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Id>0</Id>
  <Menu>Home</Menu>
  <Type>Button</Type>

</MyMenu>
Volkan Paksoy
  • 6,727
  • 5
  • 29
  • 40
Lucian Bumb
  • 2,821
  • 5
  • 26
  • 39
  • Did you look at [this](http://stackoverflow.com/questions/1212742/xml-serialize-generic-list-of-serializable-objects) and [that](http://stackoverflow.com/questions/178899/serializing-lists-of-classes-to-xml)? – Kilazur Nov 03 '15 at 14:52
  • Hard to say without seeing what the XML looks like – Volkan Paksoy Nov 03 '15 at 15:12
  • 1
    1. I understand in the serializing code model.GetType() -> model is a single MyMenu item so you have to have a collection here otherwise it will naturally deserialize to a single item. 2. Did you manually write this XML? The openning tags don't match with closing ones. (Menu -> Action, Type -> Controller) – Volkan Paksoy Nov 03 '15 at 16:30
  • 1. yes is a single; 2, yes, I was trying to extend the model, and I copy&past the wrong file. Now its clear, how to make it work. Thank you. – Lucian Bumb Nov 03 '15 at 17:10
  • I posted an answer. It's not clear in the question what RibbonMenu class is. So if you need that one too I can revise my answer – Volkan Paksoy Nov 03 '15 at 17:26
  • No your example is clear – Lucian Bumb Nov 03 '15 at 17:27

1 Answers1

0

When serializing you should initialize a collection like this:

var model = new List<MyMenu>()
{
    new MyMenu() { Name = "Menu1", Type = "Ribbon" },
    new MyMenu() { Name = "Menu2", Type = "Ribbon" },
};

This way, when you serialize you'd get something like this:

<?xml version="1.0" encoding="utf-8"?>
<ArrayOfMyMenu xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <MyMenu>
    <Name>Menu1</Name>
    <Type>Ribbon</Type>
  </MyMenu>
  <MyMenu>
    <Name>Menu2</Name>
    <Type>Ribbon</Type>
  </MyMenu>
</ArrayOfMyMenu>

And you can get the object back by using List as the type of serializer:

XmlSerializer serial = new XmlSerializer(typeof(List<MyMenu>));
System.IO.StreamReader reader = new System.IO.StreamReader("ribbonmenu.xml");
var menu = (List<MyMenu>)serial.Deserialize(reader);
reader.Close(); 

Hope this helps.

Volkan Paksoy
  • 6,727
  • 5
  • 29
  • 40