views:

57

answers:

3

This is XML document:

<?xml version="1.0" encoding="utf-8" ?>
 <ToolBars>
   <ToolBarSet id="1" buttonsCounter="4"  width="252">
      <ToolBarItem id="1" Command="Command11" Icon="pic11" Enabled="true" Visible="true" />
      <ToolBarItem id="2" Command="Command12" Icon="pic12" Enabled="true" Visible="true" />
      <ToolBarItem id="3" Command="Command13" Icon="pic13" Enabled="true" Visible="true" />
      <ToolBarItem id="4" Command="Command14" Icon="pic14" Enabled="false" Visible="true" />      
    </ToolBarSet>
    <ToolBarSet  id="2" buttonsCounter="2"  width="170">
      <ToolBarItem id="1" Command="Command21" Icon="pic11" Enabled="true" Visible="true" />
      <ToolBarItem id="2" Command="Command22" Icon="pic22" Enabled="true" Visible="true" /> 
    </ToolBarSet>
  </ToolBars>

I want to fill the appropriate classes, this is my code:
("publics" are just for test)

class Program
    {
        static void Main(string[] args)
        {
            ToolBars test;

            XmlSerializer mySerializer = new XmlSerializer(typeof(ToolBars));
            using (FileStream myFileStream = new FileStream("c:\\XMLFile1.xml", FileMode.Open))
            {

                test = (ToolBars)mySerializer.Deserialize(myFileStream);
            }



        }
    }

    [Serializable]
    [System.Xml.Serialization.XmlRoot("ToolBars")]
    public class ToolBars
    {
        public int id;
        public int buttonsCounter;
        public int width;

        [XmlArray("ToolBarSet")]
        [XmlArrayItem("ToolBar", typeof(Toolbar))]
        public List<Toolbar> toolbars = new List<Toolbar>();             

    }

    [Serializable]
    public class Toolbar
    {      
        [XmlArray("ToolbarItem")]
        [XmlArrayItem("ToolbarItem", typeof(ToolbarItem))]
        public List<ToolbarItem> toolbarItems = new List<ToolbarItem>();

    }

    [Serializable]
    public class ToolbarItem
    {
        public string command;

        public int id;

        public string icon;

        public bool visible;

        public bool enabled;

    }

How to make it work?

A: 

Just replace your classes with these:

[XmlRoot("ToobBars")]
public class ToolBars : List<ToolbarSet>
{

}

public class ToolbarSet
{
    [XmlAttribute]
    public int id { get; set; }
    [XmlAttribute]
    public int buttonsCounter { get; set; }
    [XmlAttribute]
    public int width { get; set; }

    public List<ToolbarItem> ToolBarItems = new List<ToolbarItem>();
}

public class ToolbarItem
{
    [XmlAttribute]
    public string command { get; set; }
    [XmlAttribute]
    public int id { get; set; }
    [XmlAttribute]
    public string icon { get; set; }
    [XmlAttribute]
    public bool visible { get; set; }
    [XmlAttribute]
    public bool enabled { get; set; }
}
Ciwee
this does not work
samuel
@marc_s no errors, the objects remain empty
samuel
A: 

You can use this XML format:

<?xml version="1.0" encoding="utf-8" ?>
<ToolBarConfiguration>
  <ToolBars>
    <ToolBarSet id="1" buttonsCounter="4" width="252">
      <ToolBarItems>
        <ToolBarItem 
          id="1" 
          Command="Command11" Icon="pic11" Enabled="true" Visible="true"/>
        <ToolBarItem 
          id="2" 
          Command="Command12" Icon="pic12" Enabled="true" Visible="true"/>
        <ToolBarItem 
          id="3" 
          Command="Command13" Icon="pic13" Enabled="true" Visible="true"/>
        <ToolBarItem 
          id="4" 
          Command="Command14" Icon="pic14" Enabled="false" Visible="true"/>
      </ToolBarItems>
    </ToolBarSet>
    <ToolBarSet id="2" buttonsCounter="2"  width="170">
      <ToolBarItems>
        <ToolBarItem 
          id="1" 
          Command="Command21" Icon="pic11" Enabled="true" Visible="true"/>
        <ToolBarItem 
          id="2" 
          Command="Command22" Icon="pic22" Enabled="true" Visible="true"/>
      </ToolBarItems>
    </ToolBarSet>
  </ToolBars>
</ToolBarConfiguration>

with these classes:

[Serializable]
[XmlRoot("ToolBarConfiguration")]
public class ToolBars
{
    [XmlArray("ToolBars")]
    [XmlArrayItem("ToolBarSet", typeof(Toolbar))]
    public List<Toolbar> toolbars = new List<Toolbar>();
}

[Serializable]
public class Toolbar
{
    [XmlAttribute("id")]public int id;
    [XmlAttribute("buttonsCounter")]public int buttonsCounter;
    [XmlAttribute("width")]public int width;

    [XmlArray("ToolBarItems")]
    [XmlArrayItem("ToolBarItem", typeof(ToolbarItem))]
    public List<ToolbarItem> toolbarItems = new List<ToolbarItem>();
}

[Serializable]
public class ToolbarItem
{
    [XmlAttribute("Command")]public string command;
    [XmlAttribute("id")]public int id;
    [XmlAttribute("Icon")]public string icon;
    [XmlAttribute("Visible")]public bool visible;
    [XmlAttribute("Enabled")]public bool enabled;
}

Edit: In your question you also say:

("publics" are just for test)

Be aware that for XML serialization, only the public properties and fields of an object are serialized.

João Angelo
+1  A: 

What you can always do in such a case is

  • take the XML file and run it through xsd.exe on the command line -> this gives you a XSD schema file
  • take that newly created XSD file and run it through xsd.exe with the /c parameter -> this gives you a C# file (use /l:VB in addition if you want VB.NET) that will be able to deserialize your XML

As long as you don't get any fatal errors during the two runs of xsd.exe, you should be good to go and should be able to deserialize any XML into C# objects in a second.

Marc

marc_s
I'm hating myself now for having lost so much time trying to create c# files by hand that would "fit" for an xml. Thank you.. I'll try this tool
Ciwee
I thought about using this but decided not to use any external tools, also the xml files can be changed and so will require runing the tool again on run time.
samuel
Well, if the XML is bound to change, using xsd.exe will save you even more time!
marc_s
@marc_s, but this will require me to run xsd tool every time, right?
samuel
Depends - worst case: yes. But if you don't use Xsd, you'll have to spend time figuring out what to change in your code on your own - I bet that'll take more time than running xsd.exe twice......
marc_s
and how would i "ship" the project (control)? with xsd tool?
samuel