反序列化,typeof的值 [英] Deserialization, value of typeof
问题描述
您好!
我制作了这段代码:(视觉工作室,新项目,视觉c#,wpf应用程序)
Hello!
I made this code: (visual studio, new project, visual c#, wpf application)
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 lion4
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Book.MyMain();
}
}
public class Book
{
public string Name { get; set; }
public int ISBN { get; set; }
public static void MyMain()
{
Book book1 = new Book { Name = "Lion4", ISBN = 666 };
//string bookFileName = "lion4.xaml";
XmlSerializer serializer = new XmlSerializer(typeof(Book));
using (TextWriter writer = new StreamWriter(@"Lion4.xml"))
{
serializer.Serialize(writer, book1);
}
}
}
}
创建一个xml文件lion4.xml(序列化)。
然后我尝试使用这个文件来尝试反序列化,就像这个例子一样(只是打开文件,没有保存),在这个例子中是这样的:
如何使用XML Serializer& amp; .NET中的反序列化器(C#) - YouTube [
这是我的代码:
.cs:
which creates a xml file lion4.xml (serialized).
Then I tried to use this file to try deserialization, like on this example (just open file, no saving), sth like in this example:
How To Use The XML Serializer & Deserializer in .NET (C#) - YouTube[
Here is my code:
.cs :
using System;
using System.IO;
using System.Windows;
using Microsoft.Win32;
using System.Xml.Serialization;
namespace WpfTutorialSamples.Dialogs
{
public partial class OpenFileDialogSample : Window
{
public OpenFileDialogSample()
{
InitializeComponent();
}
private void btnOpenFile_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
if (openFileDialog.ShowDialog() == true)
txtEditor.Text = File.ReadAllText(openFileDialog.FileName);
MessageBox.Show("Hello, world!", "My App", MessageBoxButton.YesNo, MessageBoxImage.Information);
if (MessageBox.Show("Yes", "No", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
{
var path = openFileDialog.FileName;
XmlSerializer serializer = new XmlSerializer(typeof(Book));
StreamReader reader = new StreamReader(path);
var input = serializer.Deserialize(reader);
button1.Content = input;
}
}
}
}
代码.xaml:
code in .xaml:
<pre lang="HTML"><Window x:Class="WpfTutorialSamples.Dialogs.OpenFileDialogSample"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="OpenFileDialogSample" Height="300" Width="708">
<DockPanel Margin="10" Width="651">
<WrapPanel HorizontalAlignment="Center" DockPanel.Dock="Top" Margin="0,0,0,10">
<Button Name="btnOpenFile" Click="btnOpenFile_Click">Open file</Button>
</WrapPanel>
<TextBox Name="txtEditor" />
<Button Content="Button" Height="83" Name="button1" Width="592" />
</DockPanel>
</Window>
唯一的错误是typeot(Book),在示例视频中是typeof(List< ;人>)。由于文件仍然序列化并且存在,请问我应该使用什么类型的值?很多人感谢!!!
我的尝试:
XmlSerializer serializer = new XmlSerializer(typeof(Book));
推荐答案
您好,我试图尽可能简化:
.cs
Hello, I tried to simplify if possible:
.cs
<pre lang="c#">using System;
using System.IO;
using System.Windows;
using Microsoft.Win32;
using System.Xml.Serialization;
namespace ofd1
{
public partial class OpenFileDialogSample : Window
{
public OpenFileDialogSample()
{
InitializeComponent();
}
private void btnOpenFile_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
if (openFileDialog.ShowDialog() == true)
txtEditor.Text = File.ReadAllText(openFileDialog.FileName);
var path = openFileDialog.FileName;
XmlSerializer serializer= new XmlSerializer (typeof(Book));
StreamReader reader = new StreamReader(path);
var input = Convert.ToString(serializer.Deserialize(reader));
txtEditor2.Text = input;
}
}
}
.xaml
.xaml
<window x:class="ofd1.OpenFileDialogSample" xmlns:x="#unknown">
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="OpenFileDialogSample" Height="300" Width="300">
<dockpanel margin="10">
<wrappanel horizontalalignment="Center" dockpanel.dock="Top" margin="0,0,0,10">
<button name="btnOpenFile" click="btnOpenFile_Click">Open file</button>
</wrappanel>
<textbox name="txtEditor" width="129" />
<textbox name="txtEditor2" />
</dockpanel>
</window>
现在,要在第二个textBox中进行反序列化,我只需要从另一个项目中找到对象Book。我做了:解决方案资源管理器 - 添加引用,我找到了项目地图,我必须选择哪一个,此时我选择的每个都是错误的并且再次出错!!!
非常感谢,拜托,我是纯粹的初学者!尝试谷歌,但得到我知道或没有帮助解决这个问题!
Now, to get the deserialization in the second textBox, I just need to find the object Book from another project. I did: Solution Explorer- add reference, I found the project map and which one do I have to choose, at this point each I chose is wrong and gives mistake again!!!
Many thanks, please, I am the sheer beginner!!! Try to google but get al I know or does not help with that particular problem!!!
这篇关于反序列化,typeof的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!