NextPVR Forums
  • ______
  • Home
  • New Posts
  • Wiki
  • Members
  • Help
  • Search
  • Register
  • Login
  • Home
  • Wiki
  • Members
  • Help
  • Search
NextPVR Forums Public Developers v
« Previous 1 … 64 65 66 67 68 … 93 Next »
Serialization

 
  • 0 Vote(s) - 0 Average
Serialization
McBainUK
Offline

Posting Freak

Posts: 4,711
Threads: 429
Joined: Sep 2005
#1
2005-11-27, 08:58 PM
Hi all

Anyone familiar with Serialization in C#? I'm trying to save a hash table of cinema lists / film lists for my UKCinemaListings plugin but running into problems.

What can and can't you do with Serialization? Ie if you serialize a hash table does it also serialize the objects/values in the hashtable?

Any input warmly received Smile
Wiki profile
My Projects
Programs Plugin [SIZE=2](retired)
| Volume OSD Plugin (retired) | Documentation Wiki (retired)
[/SIZE]
reven
Offline

Posting Freak

Posts: 5,782
Threads: 396
Joined: Sep 2004
#2
2005-11-27, 09:20 PM
create a subclass of Hashtable like this
Code:
using System;
using System.IO;
using System.Collections;

namespace Whatever
{

    [Serializable()]
    public class MyHashtable: Hashtable
    {}
.....
}

that will make it so your hashtable can be serialised

then to deserialize the object use
Code:
SoapFormatter sf = new SoapFormatter();            
FileStream datFile = new FileStream(filename, FileMode.Open);    
MyHashtable hash = (MyHashtable )sf.Deserialize(datFile);
datFile.Close();

and to serialize it use
Code:
SoapFormatter sf = new SoapFormatter();
FileStream datFile = new FileStream(filename, FileMode.Create);
sf.Serialize(datFile, myhashobject);
datFile.Close();
myhashobject is an instance of the hashtable you created.

heres my using statements in the serialize/deserialize class
Code:
using System;
using System.IO;
using System.Collections;
using System.Xml;
using System.Xml.Serialization;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Soap;

its been over a year since ive done this so im not sure which one you have to have, so just use them all Smile
McBainUK
Offline

Posting Freak

Posts: 4,711
Threads: 429
Joined: Sep 2005
#3
2005-11-27, 10:08 PM
Does anything else need to be in the Hashtable sub class?

I'm getting the error "the constructor to deserialize an object of type UKCinemaListingsPlugin.MyHashtable was not found."

Any thoughts?
Wiki profile
My Projects
Programs Plugin [SIZE=2](retired)
| Volume OSD Plugin (retired) | Documentation Wiki (retired)
[/SIZE]
reven
Offline

Posting Freak

Posts: 5,782
Threads: 396
Joined: Sep 2004
#4
2005-11-27, 10:29 PM
yeah just create a basic class, with a basic constructor. thats what i meant by the "...". add anything else you want aswell.

this should work
Code:
using System;
using System.IO;
using System.Collections;

namespace Whatever
{

    [Serializable()]
    public class MyHashtable: Hashtable
    {
        public MyHashtable(){
        }
    }
}
McBainUK
Offline

Posting Freak

Posts: 4,711
Threads: 429
Joined: Sep 2005
#5
2005-11-27, 10:39 PM
Still the same error.

Interestingly when I just use 'Hashtable' and not my subclass it seems to serialize (files are created) but I get errors concerning object types when I try to deserialize. Is the Hashtable class not natively serializeable then?

ps thanks for your help reven
Wiki profile
My Projects
Programs Plugin [SIZE=2](retired)
| Volume OSD Plugin (retired) | Documentation Wiki (retired)
[/SIZE]
reven
Offline

Posting Freak

Posts: 5,782
Threads: 396
Joined: Sep 2004
#6
2005-11-27, 11:37 PM
yeah im pretty sure you have to extend most classes in the Collections namespace if you want to serialized them.


when do you get that error, just on deserialization or at compile time?
McBainUK
Offline

Posting Freak

Posts: 4,711
Threads: 429
Joined: Sep 2005
#7
2005-11-28, 07:13 AM
Error is on deserialization, seems to serialize fine. Will look into it more.

Was thinking could use xml to write and read files. Can seperate the scraper from the plugin in then. Thanks for your help reven.
Wiki profile
My Projects
Programs Plugin [SIZE=2](retired)
| Volume OSD Plugin (retired) | Documentation Wiki (retired)
[/SIZE]
McBainUK
Offline

Posting Freak

Posts: 4,711
Threads: 429
Joined: Sep 2005
#8
2005-11-28, 02:21 PM
The problem I think is that I'm trying to serialise a class that I've made myself and it needs to be told how to do this, (with the built in classes it knows already)

Does this sound true?

Been reading here: http://www.codeproject.com/csharp/objserial.asp ('Define Serialization functions' section)
Wiki profile
My Projects
Programs Plugin [SIZE=2](retired)
| Volume OSD Plugin (retired) | Documentation Wiki (retired)
[/SIZE]
reven
Offline

Posting Freak

Posts: 5,782
Threads: 396
Joined: Sep 2004
#9
2005-11-28, 05:44 PM
try making the objects you add to the hashtable serializable, ie

Code:
    [Serializable()]
    public class ObjectImAddingToHashtable
    {
.......
}
so just add the [Serializable()] above the class name to the object class.
McBainUK
Offline

Posting Freak

Posts: 4,711
Threads: 429
Joined: Sep 2005
#10
2005-11-28, 07:37 PM
I've tried that. Don't really get serializations so going to use text files and write the required save/load routines myself (this is how i used to do it with good old basic Smile )

Learning C# and .net as I go. Will come back to serialization, but I for now I really want to add the ability to localy save cinema listings for my plugin.

Thanks for your replies reven.
Wiki profile
My Projects
Programs Plugin [SIZE=2](retired)
| Volume OSD Plugin (retired) | Documentation Wiki (retired)
[/SIZE]
« Next Oldest | Next Newest »

Users browsing this thread: 1 Guest(s)



  • View a Printable Version
  • Subscribe to this thread
Forum Jump:

© Designed by D&D, modified by NextPVR - Powered by MyBB

Linear Mode
Threaded Mode