Click here to Skip to main content
15,915,164 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
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);

            }
        }

    }
}


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 &amp; Deserializer in .NET (C#) - YouTube[

Here is my code:
.cs :

C#
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;
            }
        }
    }
}


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>


The only error is typeot(Book), which in the example video is typeof(List<person>). As the file is still serialized and existing, please, what ttypeof value should I use? MANY THANKS!!!

What I have tried:

XmlSerializer serializer = new XmlSerializer(typeof(Book));
Posted
Updated 17-Jun-16 6:07am
v2
Comments
Sergey Alexandrovich Kryukov 17-Jun-16 4:18am    
Better don't use obsolete XmlSerializer, which was never been good enough, use Data Contract.
—SA
Member 12581582 17-Jun-16 12:00pm    
Hello, the point of the tassk is to do it using XmlSerializer... :)

1 solution

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

HTML
<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>


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!!!
 
Share this answer
 
Comments
Richard MacCutchan 17-Jun-16 12:14pm    
You cannot assume that your application can find some random class that it knows nothing about. You must include the Book class in your deserialization project.
Member 12581582 20-Jun-16 6:43am    
Hello, I did this:

XmlSerializer serializer= new XmlSerializer (typeof(Book));
StreamReader reader = new StreamReader(path);
var input = Convert.ToString(serializer.Deserialize(reader));

I kKNOW that I have to include Book class in my deserialization project, but I am seraching what am I doing wrong when trying to do that, and what the right way shoudl be.

Pleae, if there are any links with similar content, can someone send? many thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900