NextPVR Forums
  • ______
  • Home
  • New Posts
  • Wiki
  • Members
  • Help
  • Search
  • Register
  • Login
  • Home
  • Wiki
  • Members
  • Help
  • Search
NextPVR Forums Public Developers v
« Previous 1 … 11 12 13 14 15 … 93 Next »
Weird Code problem - serialisation (I'll spell it the proper way!!"0

 
  • 0 Vote(s) - 0 Average
Weird Code problem - serialisation (I'll spell it the proper way!!"0
psycik
Offline

Posting Freak

Posts: 5,210
Threads: 424
Joined: Sep 2005
#11
2011-11-17, 12:17 AM
Yeah mine too. I've been playing with a settings class, and have effectively had to implement the following:

Code:
List<Server> servers;

[XmlIgnore()]
[Browsable(false)]
public List<Server> Servers { get; set }


public Server[] ServerArray { get return this.servers.ToArray(); }

This is kinda an example. So this code is in a settings class.

If I serialise the List<Server> object directly it works.
But trying to serialise the Settings class (which this internal list) fails - unless I do what I've done here and convert it to an array.
whurlston
Offline

Posting Freak

Posts: 7,885
Threads: 102
Joined: Nov 2006
#12
2011-11-17, 05:47 PM
I do it slightly different:

Code:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace XmlTests
{
    [System.Xml.Serialization.XmlRootAttribute("settings", Namespace = "", IsNullable = false)]
    public partial class Settings
    {
        private List<Server> servers = new List<Server>();

        public Settings() { }

        //[System.Xml.Serialization.XmlArrayItem("server", typeof(Server), Form=System.Xml.Schema.XmlSchemaForm.Unqualified), System.Xml.Serialization.XmlArray("servers", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
        [System.Xml.Serialization.XmlElementAttribute("server", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
        public List<Server> Servers
        {
            get
            {
                return this.servers;
            }
            set
            {
                this.servers = value;
            }
        }

        public void WriteTo(string FileName)
        {
            FileInfo fi = new FileInfo(FileName);
            using (System.IO.FileStream fs = fi.Create())
            {
                using (System.Xml.XmlTextWriter txw = new System.Xml.XmlTextWriter(fs, Encoding.GetEncoding("ISO-8859-1")))
                {
                    System.Xml.Serialization.XmlSerializerNamespaces customNamespace = new System.Xml.Serialization.XmlSerializerNamespaces(); customNamespace.Add("", "");
                    System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(this.GetType());
                    txw.Formatting = System.Xml.Formatting.Indented;
                    txw.Indentation = 2;
                    txw.WriteStartDocument();
                    serializer.Serialize(txw, this, customNamespace);
                }
            }
        }

        public void ReadFrom(string FileName)
        {
            FileInfo fi = new FileInfo(FileName);
            if (fi.Exists)
            {
                using (System.IO.FileStream fs = fi.OpenRead())
                {
                    System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(this.GetType());
                    System.Xml.XmlReaderSettings rs = new System.Xml.XmlReaderSettings();
                    rs.ProhibitDtd = false;
                    rs.XmlResolver = null;
                    System.Xml.XmlReader reader = System.Xml.XmlReader.Create(fs, rs);
                    Settings tmp = (Settings)serializer.Deserialize(reader);
                    this.Servers = tmp.Servers;
                }
            }
        }
    }

    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
    public partial class Server
    {
        private string name = string.Empty;

        public Server() { }
        public Server(string Name) { this.name = Name; }

        [System.Xml.Serialization.XmlElementAttribute("name")]
        public string Name
        {
            get { return this.name; }
            set { this.name = value; }
        }
    }

}
produces/reads the following:
Code:
<?xml version="1.0" encoding="iso-8859-1"?>
<settings>
  <server>
    <name>Test Server</name>
  </server>
  <server>
    <name>Test Server 2</name>
  </server>
</settings>
uncommenting the XmlArrayItem line (and removing the XmlElementAttribute line below it) groups the server list (adds a "<servers>" tag around the list of "<server>":
Code:
<?xml version="1.0" encoding="iso-8859-1"?>
<settings>
  <servers>
    <server>
      <name>Test Server</name>
    </server>
    <server>
      <name>Test Server 2</name>
    </server>
  </servers>
</settings>
imilne
Offline

Posting Freak

Posts: 2,423
Threads: 135
Joined: Feb 2008
#13
2011-11-17, 06:20 PM
Just as an aside, you might find this interesting reading regarding the spelling of serialization: http://en.wikipedia.org/wiki/Oxford_spelling

These kind of little annoyances cause me no end of hassle, because my day job involves visualization/visualisation.

Iain
psycik
Offline

Posting Freak

Posts: 5,210
Threads: 424
Joined: Sep 2005
#14
2011-11-17, 06:49 PM
@whurlston I'll have a toodle with that. Apart from some of the switches etc you had, my serialise method was pretty similar.

Although I ran it as a static method rather than a class method.
whurlston
Offline

Posting Freak

Posts: 7,885
Threads: 102
Joined: Nov 2006
#15
2011-11-17, 07:02 PM
Static should work too. I do that with some of my serializations. I just put that one together as a quick example.
psycik
Offline

Posting Freak

Posts: 5,210
Threads: 424
Joined: Sep 2005
#16
2011-11-17, 07:05 PM
That's what I expected, but I always kept getting the inner exception of "Object reference not found" and it doesn't tell you what what empty.

Setting the List to non null made not difference.
whurlston
Offline

Posting Freak

Posts: 7,885
Threads: 102
Joined: Nov 2006
#17
2011-11-17, 07:21 PM (This post was last modified: 2011-11-17, 07:29 PM by whurlston.)
These work in the above Settings class:
Code:
public static void WriteTo(Settings settings, string FileName)
        {
            FileInfo fi = new FileInfo(FileName);
            using (System.IO.FileStream fs = fi.Create())
            {
                using (System.Xml.XmlTextWriter txw = new System.Xml.XmlTextWriter(fs, Encoding.GetEncoding("ISO-8859-1")))
                {
                    System.Xml.Serialization.XmlSerializerNamespaces customNamespace = new System.Xml.Serialization.XmlSerializerNamespaces(); customNamespace.Add("", "");
                    System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(settings.GetType());
                    txw.Formatting = System.Xml.Formatting.Indented;
                    txw.Indentation = 2;
                    txw.WriteStartDocument();
                    serializer.Serialize(txw, settings, customNamespace);
                }
            }
        }

        public static Settings ReadFromStatic(string FileName)
        {
            try
            {
                FileInfo fi = new FileInfo(FileName);
                if (fi.Exists)
                {
                    using (System.IO.FileStream fs = fi.OpenRead())
                    {
                        System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(Settings));
                        System.Xml.XmlReaderSettings rs = new System.Xml.XmlReaderSettings();
                        rs.ProhibitDtd = false;
                        rs.XmlResolver = null;
                        System.Xml.XmlReader reader = System.Xml.XmlReader.Create(fs, rs);
                        Settings tmp = (Settings)serializer.Deserialize(reader);
                        return tmp;
                    }
                }
            }
            catch { }
            return null;
        }

The WriteTo method includes a Settings parameter since you can use "this" in a static method. The ReadFromStatic method (renamed so as not to conflict with the non-static method) returns a Settings object or null if serialization fails.
Code:
XmlTests.Settings settings = new XmlTests.Settings();
            settings.Servers = new List<XmlTests.Server>();
            settings.Servers.Add(new XmlTests.Server("Test Server"));
            settings.Servers.Add(new XmlTests.Server("Test Server 2"));

            // Non-Static methods
            settings.WriteTo("settings.xml");
            XmlTests.Settings settings2 = new XmlTests.Settings();
            settings2.ReadFrom("settings.xml");
            //These last two could actually be combined  a constructor: XmlTests.Settings settings2 = new XmlTests.Settings("settings.xml");


            // Static methods
            XmlTests.Settings.WriteTo(settings, "settings.xml");
            XmlTests.Settings settings2 = XmlTests.Settings.ReadFromStatic("settings.xml");
whurlston
Offline

Posting Freak

Posts: 7,885
Threads: 102
Joined: Nov 2006
#18
2011-11-18, 03:55 AM
Well that was one of those "Doh" moments. You said it worked in a form but not a plugin and guess where I tested all the above code. :o

I do have code that works though (I used it for my original Netflix plugin). As soon as I find it, I'll let you know what the differences are.

I did however track down what is throwing the error. When the serializer is disposed, it references a null object. Unfortunately, that is in the System.Xml.dll file and not something we can readily fix.
psycik
Offline

Posting Freak

Posts: 5,210
Threads: 424
Joined: Sep 2005
#19
2011-11-18, 04:02 AM
Great to see its not just me then getting the error.
whurlston
Offline

Posting Freak

Posts: 7,885
Threads: 102
Joined: Nov 2006
#20
2011-11-21, 07:39 AM
I checked my old netflix code and I do it the same way so I'm not sure why it doesn't work now. Strangely, the following code works:
Code:
[XmlRoot(IsNullable = false, Namespace = "")]
    public class Lists
    {
        private List<string> _Locations = new List<string>();

        [XmlArrayItem("Url", Form = XmlSchemaForm.Unqualified)]
        [XmlArray("Locations")]
        public List<string> Locations
        {
            get
            {
                return this._Locations;
            }
            set
            {
                this._Locations = value;
            }
        }

        public void ReadFrom(string FileName)
        {
            XmlSerializer xmlSerializer = new XmlSerializer(this.GetType());
            FileStream fileStream = new FileStream(FileName, FileMode.Open);
            if (fileStream.Length > 0L)
            {
                XmlReader xmlReader = XmlReader.Create((System.IO.Stream)fileStream, new XmlReaderSettings()
                {
                    ProhibitDtd = false,
                    XmlResolver = (XmlResolver)null
                });
                this._Locations = ((Lists)xmlSerializer.Deserialize(xmlReader)).Locations;
                fileStream.Close();
            }
            else
                fileStream.Close();
        }

        public void WriteTo(string FileName)
        {
            XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
            namespaces.Add("", "");
            XmlTextWriter xmlTextWriter = new XmlTextWriter(FileName, Encoding.GetEncoding("ISO-8859-1"));
            XmlSerializer xmlSerializer = new XmlSerializer(this.GetType());
            xmlTextWriter.Formatting = Formatting.Indented;
            xmlTextWriter.Indentation = 2;
            xmlTextWriter.WriteStartDocument();
            xmlSerializer.Serialize((XmlWriter)xmlTextWriter, (object)this, namespaces);
            xmlTextWriter.Close();
        }
    }
The only real difference being is that the list contains strings and not a class.
« Next Oldest | Next Newest »

Users browsing this thread: 1 Guest(s)

Pages (3): « Previous 1 2 3 Next »


Possibly Related Threads…
Thread Author Replies Views Last Post
  Remote control example code? drmargarit 4 3,931 2018-04-21, 11:24 PM
Last Post: drmargarit
  Looking for C# UPnP Media Server code bgowland 5 7,773 2016-12-16, 08:25 PM
Last Post: mvallevand
  Problem with preview image for Pending Recordings Northpole 2 2,084 2012-08-14, 02:56 AM
Last Post: Northpole
  ActivatePopup: SSPlus problem ACTCMS 10 4,970 2012-07-18, 09:43 PM
Last Post: ACTCMS
  Resolving required plugin assemblies problem McBainUK 37 12,502 2011-10-26, 06:12 PM
Last Post: psycik
  sample video overlay plugin source code? reven 2 2,456 2011-10-03, 12:42 AM
Last Post: reven
  C# code to get data directory? McBainUK 2 2,835 2011-09-19, 07:57 PM
Last Post: McBainUK
  Source code for older versions ? Spark 1 1,803 2011-02-23, 01:19 AM
Last Post: sub
  Problem using UIStatic philcooling 13 4,603 2010-02-12, 06:34 PM
Last Post: whurlston
  special MVP code? scb147 31 8,632 2009-09-04, 02:50 AM
Last Post: scb147

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

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

Linear Mode
Threaded Mode