Question

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();
         }
        }
Was it helpful?

Solution

You forgot to specify root attribute while de-serializing:

XmlSerializer deserializer = new XmlSerializer(typeof(List<Tool>) , new XmlRootAttribute("Tools"));

OTHER TIPS

You could wrap the List in a simple container, then deserialize this.

[XmlRoot("Tools")]
public class ToolList
{
    public ToolList() { Items = new List<Tool>(); }
    [XmlElement("Tool")]
    public List<Tool> Items;
}

private void ToolHandling_Loaded(object sender, RoutedEventArgs e)
{

    XmlSerializer deserializer = new XmlSerializer(typeof(ToolList));

    using (var reader = new StreamReader(@Start.userConfigurePath + 
    "\\config.xml"))
    {
    toolList = (ToolList)deserializer.Deserialize(reader);
    reader.Close();
    }
}

Based on source.

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 of List<T>

I think you can use LINQ to XML for solve this task.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top