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. Save List with BinaryReader

Save List with BinaryReader

Scheduled Pinned Locked Moved C#
question
2 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

    I have a class that has a list of objects. Since it doesn´t seem to work with xmlSerializer I´m trying to save to a binaryfile instead. When I use BinaryReader I read a string using ReadString but can I read a List or an object?

    W 1 Reply Last reply
    0
    • L larsp777

      I have a class that has a list of objects. Since it doesn´t seem to work with xmlSerializer I´m trying to save to a binaryfile instead. When I use BinaryReader I read a string using ReadString but can I read a List or an object?

      W Offline
      W Offline
      Wayne Gaylard
      wrote on last edited by
      #2

      Fisrt off, you should easily be able to serialise a List with XMLSerializer, you just need to ensure that Your custom class is marked with the XmlRoot attribute, and you create a class that inherits from List that holds your collection, something like this

      [XmlRoot("Persons")]
      public class People : List<Person>
      {

      }

      [XmlRoot("Person")]
      public class Person
      {
      public Person()
      {
      }

      public Person(string name)
      {
          Name = name;
      }
      
      \[XmlElement("Name")\]
      public string Name { get; set; }
      

      }

      then you can use this

      using (StringWriter writer = new StringWriter())
      {
      XmlSerializer serializer = new XmlSerializer(typeof(Persons));
      XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
      namespaces.Add(string.Empty, string.Empty);
      XmlWriter xmlWriter = XmlWriter.Create(writer, settings);
      serializer.Serialize(xmlWriter, personList, namespaces);
      }

      or something to that effect, and this should work to serialize to XML. If you still want to serialize to a binary file, you should use a BinaryFormatter like this

      using (Stream stream = File.Open("people.bin", FileMode.Create))
      {
      BinaryFormatter bin = new BinaryFormatter();
      bin.Serialize(stream, People);
      }

      and deserialise also using a BinaryFormatter

      using (Stream stream = File.Open("people.bin", FileMode.Open))
      {
      BinaryFormatter bin = new BinaryFormatter();
      var people = (List<Person>)bin.Deserialize(stream);
      }

      Hope this helps

      When I was a coder, we worked on algorithms. Today, we memorize APIs for countless libraries — those libraries have the algorithms - Eric Allman

      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