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. General Programming
  3. C#
  4. Problem using DataContractSerializer

Problem using DataContractSerializer

Scheduled Pinned Locked Moved C#
helpquestionlearning
7 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.
  • L Offline
    L Offline
    larsp777
    wrote on last edited by
    #1

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

    M 1 Reply Last reply
    0
    • L larsp777

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

      M Offline
      M Offline
      Member 4249997
      wrote on last edited by
      #2

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

      } }

      L 1 Reply Last reply
      0
      • M Member 4249997

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

        } }

        L Offline
        L Offline
        larsp777
        wrote on last edited by
        #3

        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?

        M 1 Reply Last reply
        0
        • L larsp777

          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?

          M Offline
          M Offline
          Member 4249997
          wrote on last edited by
          #4

          As per the subject, problem using DataContractSerializer, I have updated your contract with public properties. And the DataContract is getting serialized.

          L 1 Reply Last reply
          0
          • M Member 4249997

            As per the subject, problem using DataContractSerializer, I have updated your contract with public properties. And the DataContract is getting serialized.

            L Offline
            L Offline
            larsp777
            wrote on last edited by
            #5

            The only thing I can see you have changed is that you made the SerializerTestMethod. Did you change anything else?

            M 1 Reply Last reply
            0
            • L larsp777

              The only thing I can see you have changed is that you made the SerializerTestMethod. Did you change anything else?

              M Offline
              M Offline
              Member 4249997
              wrote on last edited by
              #6

              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>

              L 1 Reply Last reply
              0
              • M Member 4249997

                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>

                L Offline
                L Offline
                larsp777
                wrote on last edited by
                #7

                Ok, thanks!

                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