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. XML Serialization with subclass

XML Serialization with subclass

Scheduled Pinned Locked Moved C#
helpdebuggingxmljsonquestion
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.
  • K Offline
    K Offline
    Ken Mazaika
    wrote on last edited by
    #1

    Hi, I'm trying to serialize a simple configuration class that contains a few methods and primitive types. It also contains one very simple class:

    [XmlRootAttribute(ElementName = "WildAnimal", IsNullable = false)]
    class Config
    {
    public int a;
    public int b;
    public SimpleClass classinst;

    public Config()
    }
    

    class SimpleClass
    {
    public int c;
    public int d;

    public SimpleClass()
    }
    

    Along with Serialize/Deseriailze functions inside the Config class:

        public void SerializeObject(Object pObject)
        {
            try
            {
                FileStream fileStream = new FileStream(s\_strBaseThemeDirectory +
                    @"\\" +m\_strName+".xml", FileMode.CreateNew, FileAccess.ReadWrite);
                XmlSerializer xs = new XmlSerializer(typeof(Config));
                XmlTextWriter xmlTextWriter = new XmlTextWriter(fileStream, Encoding.UTF8);
    
                xs.Serialize(xmlTextWriter, pObject);
                fileStream = (FileStream)xmlTextWriter.BaseStream;
                fileStream.Close();
            }
            catch (Exception e)
            {
                Utilities.Trace("Error Serializing the XML: " + e.ToString());
            }
        }
    
        public Configuration DeserializeObject(string strName)
        {
    
            FileStream fileStream = new FileStream("filename.xml", FileMode.Open, FileAccess.ReadWrite);
    
            XmlSerializer xs = new XmlSerializer(typeof(Config));
            XmlTextWriter xmlTextWriter = new XmlTextWriter(fileStream, Encoding.UTF8);
            return (Configuration)xs.Deserialize(fileStream);
    

    }

    Everything works fine without the SimpleClass, but when that gets thrown into the mix I've been running into exceptions being thrown (the SimpleClass is actually in an Arraylist). Any idea how I should get around this issue? Thanks! -Ken

    L 1 Reply Last reply
    0
    • K Ken Mazaika

      Hi, I'm trying to serialize a simple configuration class that contains a few methods and primitive types. It also contains one very simple class:

      [XmlRootAttribute(ElementName = "WildAnimal", IsNullable = false)]
      class Config
      {
      public int a;
      public int b;
      public SimpleClass classinst;

      public Config()
      }
      

      class SimpleClass
      {
      public int c;
      public int d;

      public SimpleClass()
      }
      

      Along with Serialize/Deseriailze functions inside the Config class:

          public void SerializeObject(Object pObject)
          {
              try
              {
                  FileStream fileStream = new FileStream(s\_strBaseThemeDirectory +
                      @"\\" +m\_strName+".xml", FileMode.CreateNew, FileAccess.ReadWrite);
                  XmlSerializer xs = new XmlSerializer(typeof(Config));
                  XmlTextWriter xmlTextWriter = new XmlTextWriter(fileStream, Encoding.UTF8);
      
                  xs.Serialize(xmlTextWriter, pObject);
                  fileStream = (FileStream)xmlTextWriter.BaseStream;
                  fileStream.Close();
              }
              catch (Exception e)
              {
                  Utilities.Trace("Error Serializing the XML: " + e.ToString());
              }
          }
      
          public Configuration DeserializeObject(string strName)
          {
      
              FileStream fileStream = new FileStream("filename.xml", FileMode.Open, FileAccess.ReadWrite);
      
              XmlSerializer xs = new XmlSerializer(typeof(Config));
              XmlTextWriter xmlTextWriter = new XmlTextWriter(fileStream, Encoding.UTF8);
              return (Configuration)xs.Deserialize(fileStream);
      

      }

      Everything works fine without the SimpleClass, but when that gets thrown into the mix I've been running into exceptions being thrown (the SimpleClass is actually in an Arraylist). Any idea how I should get around this issue? Thanks! -Ken

      L Offline
      L Offline
      lisan_al_ghaib
      wrote on last edited by
      #2

      Did You added the attributes [Serializable] in classes ? this should be fix the problem : [Serializable] class Config { public int a; public int b; public SimpleClass classinst; public Config() } [Serializable] class SimpleClass { public int c; public int d; public SimpleClass() }} }

      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