开发者

deserialize XML into classes that extends collection

I deserialize XML into classes:

My XML file:

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

My class structure:

 [XmlRoot("ToolBarConfiguration")]
    public class ToolBars
    {
        [XmlArray("ToolBars")]
        [XmlArrayItem("ToolBarSet", typeof(Toolbar))]
        public List<Toolbar> Toolbars
        {
            get { return toolbars; }
            set { toolbars = value; }
        }
        List<Toolbar> toolbars = new List<Toolbar>();
    }
 public class Toolbar
    {
        [XmlAttribute("id")]
        public int Id { get; set; }
        [XmlAttribute("buttonsCounter")开发者_如何学JAVA]
        public int ButtonsCounter { get; set; }
        [XmlAttribute("width")]
        public int Width { get; set; }

        [XmlArray("ToolBarItems")]
        [XmlArrayItem("ToolBarItem", typeof(ToolbarItem))]
        public List<ToolbarItem> ToolbarItems
        {
            get { return toolbarItems; }
            set { toolbarItems = value; }
        }
        List<ToolbarItem> toolbarItems = new List<ToolbarItem>();

    }

public class ToolbarItem
    {
        [XmlAttribute("Command")]
        public string Command { get; set; }

        [XmlAttribute("id")]
        public int Id { get; set; }

        [XmlAttribute("Icon")]
        public string Icon { get; set; }

        [XmlAttribute("Icon2")]
        public string Icon2 { get; set; }

        [XmlAttribute("Visible")]
        public bool Visible { get; set; }

        [XmlAttribute("Enabled")]
        public bool Enabled { get; set; }

        [XmlAttribute("Type")]
        public ToolbarItemType ItemType { get; set; }

    }

I want to make classes more elegant,

is it a good idea to make ToolBars class extend List?

If yes, how do I need to modify the xml file and xml attributes?


What harm is it doing?

Personally, I'd leave it as is; it does what it says, and is simple to understand. That, to me, is elegant. You can also add properties at the top level later (you can't do this if it is itself a list).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜