我可以非常轻松地序列化列表:
List<String> fieldsToNotCopy =new List<String> {"Iteration Path","Iteration ID"};
fieldsToNotCopy.SerializeObject("FieldsToNotMove.xml");
现在我需要一个像这样的方法:
List<String> loadedList = new List<String();
loadedList.DeserializeObject("FieldsToNotMove.xml");
有这样的方法吗?或者我是否需要创建一个XML阅读器并以这种方式加载它?
编辑:原来没有内置的SerialzeObject。我早些时候在我的项目中做了一个而忘了它。当我发现它时,我认为它是内置的。如果你很好奇,这就是我所做的SerializeObject:
// Save an object out to the disk
public static void SerializeObject<T>(this T toSerialize, String filename)
{
XmlSerializer xmlSerializer = new XmlSerializer(toSerialize.GetType());
TextWriter textWriter = new StreamWriter(filename);
xmlSerializer.Serialize(textWriter, toSerialize);
textWriter.Close();
}
答案 0 :(得分:18)
没有像SerializeObject这样的内置方法,但编写一个方法并不是非常困难。
public void SerializeObject(this List<string> list, string fileName) {
var serializer = new XmlSerializer(typeof(List<string>));
using ( var stream = File.OpenWrite(fileName)) {
serializer.Serialize(stream, list);
}
}
反序列化
public void Deserialize(this List<string> list, string fileName) {
var serializer = new XmlSerializer(typeof(List<string>));
using ( var stream = File.OpenRead(fileName) ){
var other = (List<string>)(serializer.Deserialize(stream));
list.Clear();
list.AddRange(other);
}
}
答案 1 :(得分:4)
这些是我的序列化/反序列化扩展方法,效果很好
public static class SerializationExtensions
{
public static XElement Serialize(this object source)
{
try
{
var serializer = XmlSerializerFactory.GetSerializerFor(source.GetType());
var xdoc = new XDocument();
using (var writer = xdoc.CreateWriter())
{
serializer.Serialize(writer, source, new XmlSerializerNamespaces(new[] { new XmlQualifiedName("", "") }));
}
return (xdoc.Document != null) ? xdoc.Document.Root : new XElement("Error", "Document Missing");
}
catch (Exception x)
{
return new XElement("Error", x.ToString());
}
}
public static T Deserialize<T>(this XElement source) where T : class
{
try
{
var serializer = XmlSerializerFactory.GetSerializerFor(typeof(T));
return (T)serializer.Deserialize(source.CreateReader());
}
catch //(Exception x)
{
return null;
}
}
}
public static class XmlSerializerFactory
{
private static Dictionary<Type, XmlSerializer> serializers = new Dictionary<Type, XmlSerializer>();
public static XmlSerializer GetSerializerFor(Type typeOfT)
{
if (!serializers.ContainsKey(typeOfT))
{
System.Diagnostics.Debug.WriteLine(string.Format("XmlSerializerFactory.GetSerializerFor(typeof({0}));", typeOfT));
var newSerializer = new XmlSerializer(typeOfT);
serializers.Add(typeOfT, newSerializer);
}
return serializers[typeOfT];
}
}
您只需要为列表定义类型并使用它
public class StringList : List<String> { }
哦,你不需要XmlSerializerFactory,它就在那里,因为创建一个序列化程序很慢,如果你反复使用同一个,这会加快你的应用程序。
答案 2 :(得分:1)
我不确定这是否会对你有所帮助,但我有一些我认为与你相似的东西。
//A list that holds my data
private List<Location> locationCollection = new List<Location>();
public bool Load()
{
//For debug purposes
Console.WriteLine("Loading Data");
XmlSerializer serializer = new XmlSerializer(typeof(List<Location>));
FileStream fs = new FileStream("CurrencyData.xml", FileMode.Open);
locationCollection = (List<Location>)serializer.Deserialize(fs);
fs.Close();
Console.WriteLine("Data Loaded");
return true;
}
这允许我将所有数据反序列化回List&lt;&gt;但我建议把它放在一个尝试 - 抓住块以确保安全。事实上,现在只是看着这个就会让我在“使用”块中重写它。
我希望这会有所帮助。
修改强>
道歉,只是注意到你试图以不同的方式做到这一点,但无论如何我都会留下答案。答案 3 :(得分:0)
反序列化对象时出现错误。错误为“ XML文档(0,0)中存在错误” 。我修改了最初由@JaredPar编写的Deserialize函数来解决此错误。对某人可能有用:
public static void Deserialize(this List<string> list, string fileName)
{
XmlRootAttribute xmlRoot = new XmlRootAttribute();
xmlRoot.ElementName = "YourRootElementName";
xmlRoot.IsNullable = true;
var serializer = new XmlSerializer(typeof(List<string>), xmlRoot);
using (var stream = File.OpenRead(fileName))
{
var other = (List<string>)(serializer.Deserialize(stream));
list.Clear();
list.AddRange(other);
}
}