Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. I can´t get any output from XML and Binary.

I can´t get any output from XML and Binary.

Scheduled Pinned Locked Moved ASP.NET
csharpdatabaselinqdesignxml
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Marc Hede
    wrote on last edited by
    #1

    I have been trying to experiment with XML and Binary files. I did so by re-writing a code I made in an ASP NET Web Application. 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;
    using System.Collections;
    using System.Collections.Generic;
    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
    
    L 1 Reply Last reply
    0
    • M Marc Hede

      I have been trying to experiment with XML and Binary files. I did so by re-writing a code I made in an ASP NET Web Application. 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;
      using System.Collections;
      using System.Collections.Generic;
      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
      
      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      It looks like you are trying to run a Console application inside a web page, which will never work. Create a simple Console application and things should work out for you. You can also step through your code with the debugger in order to examine the variables at each step.

      M 1 Reply Last reply
      0
      • L Lost User

        It looks like you are trying to run a Console application inside a web page, which will never work. Create a simple Console application and things should work out for you. You can also step through your code with the debugger in order to examine the variables at each step.

        M Offline
        M Offline
        Marc Hede
        wrote on last edited by
        #3

        Thanks. I got the binary working. With XML I get an error "cannot be serialized because it does not have a parameterless constructor" at this part:

        XmlSerializer serializer = new XmlSerializer(typeof(Keycard));

        Not sure what to change/add. This is how the code looks now after creating it inside a console application.

        using System;
        using System.Collections;
        using System.Collections.Generic;
        using System.Linq;
        using System.Xml.Serialization;
        using System.IO;
        using System.Runtime.Serialization.Formatters.Binary;

        namespace Keycard
        {
        class Class1
        {
        public static 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(@"C\\Brugere\\Marc8\\source\\repos\\keycards.xml"))
                {
                    serializer.Serialize(tw, d1);
                }
        
                d1 = null;
        
                XmlSerializer deserializer = new XmlSerializer(typeof(Keycard));
                TextReader reader = new StreamReader(@"C\\Brugere\\Marc8\\source\\repos\\keycards.xml");
                object obj = deserializer.Deserialize(reader);
                d1 = (Keycard)obj;
                reader.Close();
                Console.WriteLine(d1.ToString());
            }
        
        }
        

        }

        L 1 Reply Last reply
        0
        • M Marc Hede

          Thanks. I got the binary working. With XML I get an error "cannot be serialized because it does not have a parameterless constructor" at this part:

          XmlSerializer serializer = new XmlSerializer(typeof(Keycard));

          Not sure what to change/add. This is how the code looks now after creating it inside a console application.

          using System;
          using System.Collections;
          using System.Collections.Generic;
          using System.Linq;
          using System.Xml.Serialization;
          using System.IO;
          using System.Runtime.Serialization.Formatters.Binary;

          namespace Keycard
          {
          class Class1
          {
          public static 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(@"C\\Brugere\\Marc8\\source\\repos\\keycards.xml"))
                  {
                      serializer.Serialize(tw, d1);
                  }
          
                  d1 = null;
          
                  XmlSerializer deserializer = new XmlSerializer(typeof(Keycard));
                  TextReader reader = new StreamReader(@"C\\Brugere\\Marc8\\source\\repos\\keycards.xml");
                  object obj = deserializer.Deserialize(reader);
                  d1 = (Keycard)obj;
                  reader.Close();
                  Console.WriteLine(d1.ToString());
              }
          
          }
          

          }

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          You need to add the parameterless constructor to your Keycard class as directed by the error message:

          public Keycard()
          {
          // may need default values rather than null at this point.
          this.Name = null;
          this.Mykey = null;
          }

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups