Click here to Skip to main content
14,695,503 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:

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()


    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);



and here is content of "Lion6.xml" file:

<?xml version="1.0" encoding="UTF-8"?>

-<Book xmlns:xsd="" xmlns:xsi="">




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);
Updated 15-Jun-16 7:48am
Richard Deeming 15-Jun-16 11:37am
This is now your THIRD copy of the same question:[^][^]

You have already been given the answer. It hasn't changed in the last 24 hours.
Rob Philpott 15-Jun-16 11:40am
It won't show anything in the main window as there's no coding doing so. What you program appears to do is create a book, serialise, deserialise it (effectively cloning) and stops there.

You'd need to create some mark up and binding or actually start putting controls on the screen to display it.
Member 12581582 15-Jun-16 11:43am

1 solution

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.


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

  Print Answers RSS
Top Experts
Last 24hrsThis month

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