这是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;
}
如何让它发挥作用?
答案 0 :(得分:1)
在这种情况下你总是做的是
xsd.exe
运行它 - &gt;这为您提供了XSD架构文件xsd.exe
参数{ - 1}}通过/c
运行该文件。这将为您提供一个C#文件(如果您想要VB.NET,还可以使用/ l:VB),它将能够反序列化您的XML 只要你在两次运行xsd.exe期间没有遇到任何致命错误,你应该好好去,并且应该能够在一秒钟内将任何XML反序列化为C#对象。
马克
答案 1 :(得分:0)
只需用以下内容替换您的类:
[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; }
}
答案 2 :(得分:0)
您可以使用以下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序列化,只会序列化对象的公共属性和字段。