代码之家  ›  专栏  ›  技术社区  ›  samuel

对象的XML反序列化组

  •  0
  • samuel  · 技术社区  · 16 年前

    这是XML文档:

    <?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>
    

    我想填充适当的类,这是我的代码:
    (公众只是为了测试)

    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;
    
        }
    

    如何使其工作?

    3 回复  |  直到 16 年前
        1
  •  0
  •   João Angelo    16 年前

    可以使用此XML格式:

    <?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>
    

    对于这些类:

    [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;
    }
    

    编辑: 在你的问题中,你也会说:

    (公众只是为了测试)

    请注意,对于XML序列化,只序列化对象的公共属性和字段。

        2
  •  1
  •   marc_s MisterSmith    16 年前

    你能做什么 总是 在这种情况下做的是

    • 获取XML文件并运行它 xsd.exe 在命令行上->这将为您提供一个XSD架构文件
    • 获取新创建的XSD文件并运行它 XSD-EXE /c 参数->这将为您提供一个C文件(如果需要vb.net,还可以使用/L:vb),该文件将能够反序列化XML

    只要在xsd.exe的两次运行过程中没有出现任何致命错误,就应该可以很好地执行,并且应该能够在一秒钟内将任何XML反序列化为C对象。

    马克

        3
  •  0
  •   Andre Pena    16 年前

    只需将类替换为以下内容:

    [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; }
    }