CSharp - 序列化后,XML不能反序列化

  显示原文与译文双语对照的内容
0 0

创建 List,将它的保存为 XML ( 使用 XmlSerializer ),但我没有成功的( 尽管所有网络搜索。) 来反序列化。

我的实体是:


public class basicTxtFile
{
 public string filename;
 public string description;
}

public class fileTools
{
};

public class textboxTool : fileTools//text box
{
 public string defaultText;
 public bool multiLine;
 public bool browseButton;
};

public class comboboxTool : fileTools//combo box
{
 public List<string> values = new List<string>();
};

//Must file, can choose tools: textbox andor combobox
public class mustFiles : basicTxtFile
{
 public List<fileTools> mustTools = new List<fileTools>();
};

public class OptionalFiles : mustFiles
{
 public bool exist;//checkbox for defualt value - if the file is exist, if not.
};

在我的应用程序中,我装入一个 List 并手动填充它。 之后,我将它保存在下面的代码中:


//Save list into XML - success
XmlSerializer serializer = new XmlSerializer(typeof(List<mustFiles>), new Type[] {typeof(fileTools), typeof(textboxTool), typeof(comboboxTool)});

using (FileStream stream = File.OpenWrite("MustFiles.xml"))
{
 serializer.Serialize(stream, mustTxtFiles);
}

然后尝试将该xml文件加载到 List 中,但由于以下原因而失败: "XML文档中存在错误( 2,2 ) 。"innerException ="不应为 尽管XML文件自动生成。

我的加载代码是:


//Load XML file into list
List<mustFiles> mustTry = new List<mustFiles>();
mustTry = bl.loadXmlIntoList<mustFiles>("MustFiles.xml","mustFiles");

loadXmlIntoList函数:


public List<T> loadXmlIntoList<T>(string xmlFileName, string xmlElemnetName)
{
 XmlRootAttribute xRoot = new XmlRootAttribute();
 xRoot.ElementName = xmlElemnetName;
 xRoot.IsNullable = true;

 XmlSerializer serializer = new XmlSerializer(typeof(T), xRoot);

 using (FileStream stream = File.OpenRead(xmlFileName))
 {
 List<T> dezerializedList = (List<T>)serializer.Deserialize(stream);
 return dezerializedList;
 }
}

我的问题 我错了? 如何将xml文件加载到 List 中?

谢谢!

XML文件( 自动生成) 如下所示:


<?xml version="1.0"?>
<ArrayOfMustFiles xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
 <mustFiles>
 <filename>file1.txt</filename>
 <description>desc1</description>
 <mustTools>
 <fileTools xsi:type="textboxTool">
 <defaultText>Default text 01</defaultText>
 <multiLine>false</multiLine>
 <browseButton>false</browseButton>
 </fileTools>
 </mustTools>
 </mustFiles>
 <mustFiles>
 <filename>file2.txt</filename>
 <description>desc2</description>
 <mustTools>
 <fileTools xsi:type="textboxTool">
 <defaultText>Defualt text 02</defaultText>
 <multiLine>true</multiLine>
 <browseButton>true</browseButton>
 </fileTools>
 <fileTools xsi:type="comboboxTool">
 <values>
 <string>Val1</string>
 <string>Val2</string>
 <string>Val3</string>
 </values>
 </fileTools>
 </mustTools>
 </mustFiles>
 <mustFiles>
 <filename>file2.txt</filename>
 <description>desc2</description>
 <mustTools>
 <fileTools xsi:type="textboxTool">
 <defaultText>Defualt text 03</defaultText>
 <multiLine>false</multiLine>
 <browseButton>true</browseButton>
 </fileTools>
 <fileTools xsi:type="comboboxTool">
 <values>
 <string>ComboVal 1</string>
 <string>ComboVal 2</string>
 <string>ComboVal 3</string>
 </values>
 </fileTools>
 <fileTools xsi:type="comboboxTool">
 <values>
 <string>Second ComboVal 1</string>
 <string>Second ComboVal 2</string>
 <string>Second ComboVal 3</string>
 </values>
 </fileTools>
 <fileTools xsi:type="textboxTool">
 <defaultText>Second defualt text 03</defaultText>
 <multiLine>true</multiLine>
 <browseButton>false</browseButton>
 </fileTools>
 </mustTools>
 </mustFiles>
</ArrayOfMustFiles>

更新: 我还尝试向实体添加 {get; set;},如下所示:


public class basicTxtFile
{
 public string filename{ set; get; }
 public string description{ set; get; }
}

public class fileTools
{ };

public class textboxTool : fileTools
{
 public string defaultText{ set; get; }
 public bool multiLine{ set; get; }
 public bool browseButton{ set; get; }
};

public class comboboxTool : fileTools
{
 public List<string> values { set; get; }
 public comboboxTool()
 {
 values = new List<string>();
 }
};

public class mustFiles : basicTxtFile
{
 public List<fileTools> mustTools { set; get; }
 public mustFiles()
 {
 mustTools = new List<fileTools>();
 }
};

时间: 原作者:

0 0

我不是一个XML专家。 你想用 loadXmlIntoList() 中的XmlRootAttribute做什么?

为了使反序列化代码看起来更像它的序列化,我稍微修改了它:


public partial class Form1 : Form
{
 public Form1()
 {
 InitializeComponent();
 }

 private void button1_Click(object sender, EventArgs e)
 {
 List<mustFiles> mustTxtFiles = new List<mustFiles>();

 mustFiles mf = new mustFiles();
 mf.filename ="filenameA";
 mf.description ="descriptionA";
 textboxTool tbt = new textboxTool();
 tbt.defaultText ="defaultTextA";
 tbt.browseButton = true;
 tbt.multiLine = true;
 mf.mustTools.Add(tbt);
 mustTxtFiles.Add(mf);

 mf = new mustFiles();
 mf.filename ="filenameB";
 mf.description ="descriptionB";
 tbt = new textboxTool();
 tbt.defaultText ="defaultTextB";
 tbt.browseButton = true;
 tbt.multiLine = true;
 mf.mustTools.Add(tbt);
 mustTxtFiles.Add(mf);

//serialize it
 XmlSerializer serializer = new XmlSerializer(typeof(List<mustFiles>), new Type[] {typeof(fileTools), typeof(textboxTool), typeof(comboboxTool)});
 string xmlFile = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),"MustFiles.xml");
 using (System.IO.FileStream stream = File.OpenWrite(xmlFile))
 {
 serializer.Serialize(stream, mustTxtFiles);
 }

//Why not just this?
//deserialize it 
//List<mustFiles> mustTry;
//using (FileStream stream = File.OpenRead(xmlFile))
//{
//mustTry = (List<mustFiles>)serializer.Deserialize(stream);
//}

//deserialize it with generic function:
 List<mustFiles> mustTry = loadXml<List<mustFiles>>(xmlFile, new Type[] { typeof(fileTools), typeof(textboxTool), typeof(comboboxTool) });
 }

 public T loadXml<T>(string xmlFileName, Type[] additionalTypes)
 {
 XmlSerializer serializer = new XmlSerializer(typeof(T), additionalTypes);
 using (FileStream stream = File.OpenRead(xmlFileName))
 {
 return (T)serializer.Deserialize(stream);
 }
 }

}

原作者:
...