我创建了一个列表,将其保存为 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 and\or 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.
};
在我的应用程序中,我创建了一个列表并手动填写它。之后我用这段代码保存了它:
// 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 文件加载到列表中,但由于 “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 文件加载到列表中?
谢谢!
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>();
}
};