序列化正在运行,但反序列化不是吗? [英] Serialization is working, but deserialization not?
问题描述
您好!
我在visual studio 2010终极版,新项目中工作 - > viusal c#,WpfApplication。
我有一个序列化代码,它可以工作。
这是代码:
Hello!
I work in visual studio 2010 ultimate, new project-> viusal c#, WpfApplication.
I have a code for serialization and it Works.
Here is the code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using System.Xml.Serialization;
namespace lion6
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
Book.MyMain();
}
}
public class Book
{
public string Name { get; set; }
public int ISBN { get; set; }
public static void MyMain()
{
Book book1 = new Book { Name = "Lion6", ISBN = 666 };
//string bookFileName = "lion4.xaml";
XmlSerializer serializer = new XmlSerializer(typeof(Book));
using (TextWriter writer = new StreamWriter(@"Lion6.xml"))
{
serializer.Serialize(writer, book1);
}
XmlSerializer deserialize = new XmlSerializer(typeof(Book));
TextReader input = new StreamReader(@"Lion6.xml");
Book bookl6 = (Book)deserializer.Deserialize(input);
}
}
}
这里是Lion6.xml文件的内容:
and here is content of "Lion6.xml" file:
<?xml version="1.0" encoding="UTF-8"?>
-<Book xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Name>Lion6</Name>
<ISBN>666</ISBN>
</Book>
问题:当我运行程序时,我得到的结果是干净的空窗口MainWindow ,没有con帐篷,当我打开它时,我只在xml文件中有内容。这意味着反序列化不好吗?反序列化是否应该在MainWindow中显示一些内容?非常感谢!!!
我尝试过:
XmlSerializer deserialize = new XmlSerializer(typeof(Book));
TextReader输入= new StreamReader(@Lion6.xml);
Book bookl6 =(Book)deserialize.Deserialize(input);
Problem: when I run the program, I get as a result clean empty window MainWindow, with no content, I have content only in xml file, when I open it. Is that meaning that deserialization is not ok? Should deserialization not show some content in the MainWindow? Many thanks!!!
What I have tried:
XmlSerializer deserialize = new XmlSerializer(typeof(Book));
TextReader input = new StreamReader(@"Lion6.xml");
Book bookl6 = (Book)deserialize.Deserialize(input);
推荐答案
对不起,我没看你的代码;我的建议是不同的:为什么使用这种过时的序列化技术,XmlSerializer
?它从来都不够好。但下一次微软尝试创建一种真正通用的,非侵入式,类型无关的序列化技术是非常出色的。它基于数据合同。这种方法不仅非常强大,而且最容易使用。请参阅:使用数据合同。
-SA
Sorry, I did not look at your code; my advice is different: why using this obsolete serialization technique,XmlSerializer
? It never was good enough. But the next Microsoft attempt to create a really universal, non-intrusive, type-agnostic serialization technology is brilliant. It is based on Data Contract. Not only this approach is very robust, but it also the easiest to use. Please see: Using Data Contracts.
—SA
这篇关于序列化正在运行,但反序列化不是吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!