C# - 反序列化列表
我可以非常容易地序列化列表:
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();
}
I can serialize a list really easy:
List<String> fieldsToNotCopy =new List<String> {"Iteration Path","Iteration ID"};
fieldsToNotCopy.SerializeObject("FieldsToNotMove.xml");
Now I need a method like this:
List<String> loadedList = new List<String();
loadedList.DeserializeObject("FieldsToNotMove.xml");
Is there such a method? Or am I going to need to create an XML reader and load it in that way?
EDIT: Turns out there is no built in SerialzeObject. I had made one earlier in my project and forgot about it. When I found it I thought it was built in. In case you are curious this is the SerializeObject that I made:
// 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();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
没有像 SerializeObject 这样的内置方法,但编写一个方法并不是非常困难。
并反序列化
There is no such builtin method as SerializeObject but it's not terribly difficult to code one up.
And Deserialize
这些是我的序列化/反序列化扩展方法,工作得很好
你只需要为你的列表定义一个类型并使用它哦
,你不需要 XmlSerializerFactory,它就在那里,因为创建序列化器很慢,如果你一遍又一遍地使用同一个,这可以加快您的应用程序的速度。
These are my serialize/deserialize extension methods that work quite well
You just need to define a type for your list and use it instead
Oh, and you don't NEED the XmlSerializerFactory, it's just there since creating a serializer is slow, and if you use the same one over and over this speeds up your app.
我不确定这是否会对你有帮助,但我有一些我认为与你相似的东西。
这允许我将所有数据反序列化回 List<> 中。但为了安全起见,我建议将其放在 try-catch 块中。事实上,现在只要看看这个就会让我在“使用”块中重写它。
我希望这有帮助。
编辑:
抱歉,刚刚注意到您正在尝试以不同的方式进行操作,但无论如何我都会将答案留在那里。
I'm not sure whether this will help you but I have dome something which I believe to be similar to you.
This allows me to deserialise all my data back into a List<> but i'd advise putting it in a try - catch block for safety. In fact just looking at this now is going to make me rewrite this in a "using" block too.
I hope this helps.
EDIT:
Apologies, just noticed you're trying to do it a different way but i'll leave my answer there anyway.
我在反序列化对象时遇到错误。错误为“XML 文档 (0, 0) 中存在错误”。我修改了最初由 @JaredPar 编写的 Deserialize 函数来解决此错误。这可能对某人有用:
I was getting error while deserializing to object. The error was "There is an error in XML document (0, 0)". I have modified the Deserialize function originally written by @JaredPar to resolve this error. It may be useful to someone:
创建要序列化的产品列表 序列
化
反序列化
Create a list of products be serialized
Serialization
Deserialization