Problem using DataContractSerializer
-
Hi! I trying to use DataContractSerializer to save objects with crossreference. It´s two lists of objects. I doesn´t get a error but even though the file is created it does´t work to get it back. Maybe it´s better to use BinaryFormatter? Some of my code:
DataContractSerializer xs = new DataContractSerializer(typeof(List));
DataContractSerializer xs2 = new DataContractSerializer(typeof(List));Book-class:
\[KnownType(typeof(FaktaBok))\] \[KnownType(typeof(BarnBok))\] \[KnownType(typeof(SportBok))\] \[DataContractAttribute()\] public class Book { \[DataMember\] protected int isbn = 0; \[DataMember\] protected string titel = null; \[DataMember\] protected string author; \[DataMember\] protected int price; \[DataMember\] protected String isType = null; \[DataMember\] protected Kund biblioteksKund = null;
One derived class:
[DataContractAttribute()]
public class SportBok : Bok
{
[DataMember]
private string sport;Serialize:
using (Stream s = File.Create(path))
{
xs.WriteObject(s, CustomerList);
}using (Stream s2 = File.Create(path2)) { xs2.WriteObject(s2, BookList); }
DeSerialize:
if (File.Exists(path))
{
using (Stream s = File.OpenRead(path))
{
CustomerList = (List)xs.ReadObject(s);
}
}if (File.Exists(path2))
{
using (Stream s2 = File.OpenRead(path2))
{
BookList = (List)xs2.ReadObject(s2);
}
} -
Hi! I trying to use DataContractSerializer to save objects with crossreference. It´s two lists of objects. I doesn´t get a error but even though the file is created it does´t work to get it back. Maybe it´s better to use BinaryFormatter? Some of my code:
DataContractSerializer xs = new DataContractSerializer(typeof(List));
DataContractSerializer xs2 = new DataContractSerializer(typeof(List));Book-class:
\[KnownType(typeof(FaktaBok))\] \[KnownType(typeof(BarnBok))\] \[KnownType(typeof(SportBok))\] \[DataContractAttribute()\] public class Book { \[DataMember\] protected int isbn = 0; \[DataMember\] protected string titel = null; \[DataMember\] protected string author; \[DataMember\] protected int price; \[DataMember\] protected String isType = null; \[DataMember\] protected Kund biblioteksKund = null;
One derived class:
[DataContractAttribute()]
public class SportBok : Bok
{
[DataMember]
private string sport;Serialize:
using (Stream s = File.Create(path))
{
xs.WriteObject(s, CustomerList);
}using (Stream s2 = File.Create(path2)) { xs2.WriteObject(s2, BookList); }
DeSerialize:
if (File.Exists(path))
{
using (Stream s = File.OpenRead(path))
{
CustomerList = (List)xs.ReadObject(s);
}
}if (File.Exists(path2))
{
using (Stream s2 = File.OpenRead(path2))
{
BookList = (List)xs2.ReadObject(s2);
}
}I have done some modification for your code -
[KnownType(typeof (FaktaBok))]
[KnownType(typeof (BarnBok))]
[KnownType(typeof (SportBok))]
[DataContractAttribute()]
public class Book
{
[DataMember]
public int IsBn = 0;\[DataMember\] public string Titel = null; \[DataMember\] public string Author; \[DataMember\] public int Price; \[DataMember\] public String IsType = null; } public void SerializerTestMethod() { List<Book> books = new List<Book> { new Book() { Author = "author1", IsBn = 1, IsType = "istype1", Price = 10, Titel = "booktitle1" }, new Book() { Author = "author2", IsBn = 2, IsType = "istype2", Price = 20, Titel = "booktitle2" } }; string writeTo = @"FilePath"; DataContractSerializer xs2 = new DataContractSerializer(typeof (List<Book>)); using (Stream s2 = File.Create(writeTo)) { xs2.WriteObject(s2, books); } List<Book> outBook = null; if (File.Exists(writeTo)) { using (Stream s2 = File.OpenRead(writeTo)) { outBook = (List<Book>) xs2.ReadObject(s2); } } }
} }
-
I have done some modification for your code -
[KnownType(typeof (FaktaBok))]
[KnownType(typeof (BarnBok))]
[KnownType(typeof (SportBok))]
[DataContractAttribute()]
public class Book
{
[DataMember]
public int IsBn = 0;\[DataMember\] public string Titel = null; \[DataMember\] public string Author; \[DataMember\] public int Price; \[DataMember\] public String IsType = null; } public void SerializerTestMethod() { List<Book> books = new List<Book> { new Book() { Author = "author1", IsBn = 1, IsType = "istype1", Price = 10, Titel = "booktitle1" }, new Book() { Author = "author2", IsBn = 2, IsType = "istype2", Price = 20, Titel = "booktitle2" } }; string writeTo = @"FilePath"; DataContractSerializer xs2 = new DataContractSerializer(typeof (List<Book>)); using (Stream s2 = File.Create(writeTo)) { xs2.WriteObject(s2, books); } List<Book> outBook = null; if (File.Exists(writeTo)) { using (Stream s2 = File.OpenRead(writeTo)) { outBook = (List<Book>) xs2.ReadObject(s2); } } }
} }
Well, the reason I didn´t even get an error was because the Form_Load didn´t fire. But now that I fixed that I get "unable to serilalize" due to elements not being closed or something like that. The application is a library and customers can lend books. What I do is save a list of books in a customer objekt so you know what books the customer borrowed. What exactly is your code doing?
-
Well, the reason I didn´t even get an error was because the Form_Load didn´t fire. But now that I fixed that I get "unable to serilalize" due to elements not being closed or something like that. The application is a library and customers can lend books. What I do is save a list of books in a customer objekt so you know what books the customer borrowed. What exactly is your code doing?
As per the subject, problem using DataContractSerializer, I have updated your contract with public properties. And the DataContract is getting serialized.
-
As per the subject, problem using DataContractSerializer, I have updated your contract with public properties. And the DataContract is getting serialized.
-
The only thing I can see you have changed is that you made the SerializerTestMethod. Did you change anything else?
When using DataContract it is advisible to use Public properties instead of protected. That is my other change ... Following is the serialized datacontract with sample data.
<ArrayOfBook xmlns="http://schemas.datacontract.org/2004/07/TestSampApp" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<Book>
<Author>author1</Author>
<IsBn>1</IsBn>
<IsType>istype1</IsType>
<Price>10</Price>
<Titel>booktitle1</Titel>
</Book>
<Book>
<Author>author2</Author>
<IsBn>2</IsBn>
<IsType>istype2</IsType>
<Price>20</Price>
<Titel>booktitle2</Titel>
</Book>
</ArrayOfBook> -
When using DataContract it is advisible to use Public properties instead of protected. That is my other change ... Following is the serialized datacontract with sample data.
<ArrayOfBook xmlns="http://schemas.datacontract.org/2004/07/TestSampApp" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<Book>
<Author>author1</Author>
<IsBn>1</IsBn>
<IsType>istype1</IsType>
<Price>10</Price>
<Titel>booktitle1</Titel>
</Book>
<Book>
<Author>author2</Author>
<IsBn>2</IsBn>
<IsType>istype2</IsType>
<Price>20</Price>
<Titel>booktitle2</Titel>
</Book>
</ArrayOfBook>