locked
What is the correct project type for XML and Binary? RRS feed

  • Question

  • User766850287 posted

    I have been using ASP.NET Web Application and webforms for all my old projects, and now I wanted to try and save one of my older projects as an XML and Binary file. I am actually not getting any errors, but I am not getting any output either. It does not show my data or create an XML file in the folder I specified. So I wonder if the reason for that is that I am using a Web Application form. What should I use instead?

    Here is the code. As I said, I get no error messages, so I don´t know if there are any mistakes in the code itself.

    Keycard.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Runtime.Serialization;
    using System.Runtime.Serialization.Formatters.Binary;
    
    namespace Keycard
    {
        [Serializable()]
        public class Keycard : ISerializable
        {
            protected string name;
            protected int mykey;
    
            public Keycard(string name, int mykey)
            {
                this.Name = name;
                this.Mykey = mykey;
            }
    
            public string Name
            {
                get { return name; }
                set { name = value; }
            }
    
            public int Mykey
            {
                get { return mykey; }
                set { mykey = value; }
            }
    
            public void GetObjectData(SerializationInfo info, StreamingContext context)
            {
    
                info.AddValue("Name", name);
                info.AddValue("Keynumber", mykey);
      
            }
            public Keycard(SerializationInfo info, StreamingContext context)
            {
                Name = (string)info.GetValue("Name", typeof(string));
                Mykey = (int)info.GetValue("Keynumber", typeof(int));
            }
    
            public override string ToString()
            {
                return "Name: " + name + " ---- " + " Keynumber: " + mykey;
            }
        }
    }
    

    index.aspx.cs

    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Configuration;
    using System.Xml.Serialization;
    using System.IO;
    using System.Runtime.Serialization.Formatters.Binary;
    
    namespace Keycard
    {
        public partial class Index : System.Web.UI.Page
        {
            public void Main(string[] args)
            {
                Keycard d1 = new Keycard("John", 102030);
    
                Stream stream = File.Open("KeycardData.dat",
                    FileMode.Create);
    
                BinaryFormatter bf = new BinaryFormatter();
    
                bf.Serialize(stream, d1);
                stream.Close();
    
                d1 = null;
    
                stream = File.Open("KeycardData.dat", FileMode.Open);
    
                bf = new BinaryFormatter();
    
                d1 = (Keycard)bf.Deserialize(stream);
                stream.Close();
                Console.WriteLine(d1.ToString());
    
                XmlSerializer serializer = new XmlSerializer(typeof(Keycard));
    
                using(TextWriter tw = new StreamWriter(@"L\C#\keycards.xml"))
                {
                    serializer.Serialize(tw, d1);
                }
    
                d1 = null;
    
                XmlSerializer deserializer = new XmlSerializer(typeof(Keycard));
                TextReader reader = new StreamReader(@"L\C#\keycards.xml");
                object obj = deserializer.Deserialize(reader);
                d1 = (Keycard)obj;
                reader.Close();
                Console.WriteLine(d1.ToString());
            }
    
        }
    }

    Sunday, December 8, 2019 4:01 PM

All replies

  • User288213138 posted

    Hi McQvist,

    public void Main(string[] args)

    If you debug your code in web form, you will find that you have not called this method.

    I suggest you can put the code in the Page_Load method. If you want to output something in a web form, you should use Response.Write () method.

     protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    Keycard d1 = new Keycard("John", 102030);
                    Stream stream = File.Open("KeycardData.dat",FileMode.Create);
                    BinaryFormatter bf = new BinaryFormatter();
                    bf.Serialize(stream, d1);
                    stream.Close();
    
                    d1 = null;
    
                    stream = File.Open("KeycardData.dat", FileMode.Open);
    
                    bf = new BinaryFormatter();
    
                    d1 = (Keycard)bf.Deserialize(stream);
                    stream.Close();
                    Console.WriteLine(d1.ToString());
    
                    XmlSerializer serializer = new XmlSerializer(typeof(Keycard));
    
                    using (TextWriter tw = new StreamWriter(@"L\C#\keycards.xml"))
                    {
                        serializer.Serialize(tw, d1);
                    }
    
                    d1 = null;
    
                    XmlSerializer deserializer = new XmlSerializer(typeof(Keycard));
                    TextReader reader = new StreamReader(@"L\C#\keycards.xml");
                    object obj = deserializer.Deserialize(reader);
                    d1 = (Keycard)obj;
                    reader.Close();
                    Response.Write(d1.ToString());
                }
            }      

    And you should add a parameterless constructor into Keycard class, Otherwise it cannot be serialized.

    public class Keycard : ISerializable
        {
            public Keycard()
            {
    
            }
         }

    This is my test result:

    Best regards,

    Sam

    Monday, December 9, 2019 6:57 AM