The serialization of a list doesn't face problems. Creation of the XML File:
XmlDocument toolConfig = new XmlDocument();
XmlNode myRoot;
myRoot = toolConfig.CreateElement("Tool");
toolConfig.AppendChild(myRoot);
toolConfig.Save(@userConfigurePath + "\\config.xml");
After the serialization the xml file looks like this:
<?xml version="1.0" encoding="utf-8"?>
<Tools xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Tool>
<Name>test</Name>
<Path>C:\Program Files\FreePDF_XP\freepdf.exe</Path>
</Tool>
<Tool>
<Name>test2</Name>
<Path>C:\Program Files\FreePDF_XP\fpconfig.exe</Path>
</Tool>
<Tool>
<Name>test3</Name>
<Path>C:\Program Files\FreePDF_XP\redrun.exe</Path>
</Tool>
</Tools>
Deserialization code:
private void ToolHandling_Loaded(object sender, RoutedEventArgs e)
{
XmlSerializer deserializer = new XmlSerializer(typeof(List<Tool>));
using (var reader = new StreamReader(@Start.userConfigurePath +
"\\config.xml"))
{
toolList = (List<Tool>)deserializer.Deserialize(reader);
reader.Close();
}
I get the XML Document Error 2,2 : System.InvalidOperationException: There is an error in XML document (2, 2). Therefore I used a validation tool for the document and I got no error . Where is the source of the error?
Edit: Full code to compose the xml:
private List<Tool> toolList = new List<Tool>();
private void ToolHandling_Closed(object sender, EventArgs e)
{
XmlSerializer serializer = new XmlSerializer(toolList.GetType(), new
XmlRootAttribute("Tools"));
using (var writer = new StreamWriter(@Start.userConfigurePath +
"\\config.xml"))
{
serializer.Serialize(writer, toolList);
writer.Close();
}
}
You forgot to specify root attribute while de-serializing:
http://msdn.microsoft.com/ru-ru/library/system.xml.serialization.xmlserializer(v=vs.110).aspx
Serialization of ArrayList and Generic List The XmlSerializer cannot serialize or deserialize the following: Arrays of
ArrayList
Arrays ofList<T>
I think you can use LINQ to XML for solve this task.
You could wrap the List in a simple container, then deserialize this.
Based on source.