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
M

Member 4249997

@Member 4249997
About
Posts
3
Topics
0
Shares
0
Groups
0
Followers
0
Following
0

Posts

Recent Best Controversial

  • Problem using DataContractSerializer
    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>

    C# help question learning

  • Problem using DataContractSerializer
    M Member 4249997

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

    C# help question learning

  • Problem using DataContractSerializer
    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);
                }
            }
            
        }
    

    } }

    C# help question learning
  • Login

  • Don't have an account? Register

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