I can´t get any output from XML and Binary.
-
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
-
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
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.
-
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.
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()); } }
}
-
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()); } }
}