XML deserialization group of objects
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" Enabl开发者_如何转开发ed="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?
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
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.
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; }
}
精彩评论