我创建一个列表,将其保存为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;}
{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>();
}
};