列表中的序列不面对的问题。 XML文件的创建:
XmlDocument toolConfig = new XmlDocument();
XmlNode myRoot;
myRoot = toolConfig.CreateElement("Tool");
toolConfig.AppendChild(myRoot);
toolConfig.Save(@userConfigurePath + "\\config.xml");
序列化之后的XML文件看起来像这样:
<?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>
反序列化的代码:
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();
}
我得到的XML文档错误2,2:System.InvalidOperationException:有是XML文档中的一个错误(2,2)。 因此,我用于文档验证工具,我没有错误。 哪里是错误的来源?
编辑:全码组成的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();
}
}