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
whurlston
Offline

Posting Freak

Posts: 7,885
Threads: 102
Joined: Nov 2006
#21
2011-11-24, 07:11 AM (This post was last modified: 2011-11-25, 03:14 AM by whurlston.)
Fixed it by using an XmlIgnore on the public List<Server> and instead serializing "public Server[] ServerArray" which just converts to/from the list object. It looks like there is a bug in the List<> implementation. I would be curious to try other versions of the .Net 2.0 framework to see when it changed (SP1/SP2), but not enought to mess with my system right now.

Code:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml.Serialization;
using System.Xml;
using System.Xml.Schema;

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

        public Settings()
        {
            // If you didn't initialize the private variable "servers" in the declaration, you must do it here.
        }

        [COLOR="#0000FF"][System.Xml.Serialization.XmlArray("servers")]
        [System.Xml.Serialization.XmlArrayItem("server", typeof(Server), IsNullable = true)][/COLOR]
        [COLOR="#008000"]// Comment out the above two lines and uncomment the next line if you don't want all the <server> elements grouped within a <servers> element
        //[System.Xml.Serialization.XmlElementAttribute("server", typeof(Server), Form = System.Xml.Schema.XmlSchemaForm.Unqualified)][/COLOR]
        [COLOR="#0000FF"]public Server[] ServersArray
        {
            get { return this.servers.ToArray(); }
            set { if (value != null) this.servers = new List<Server>(value); }
        }[/COLOR]

        [COLOR="#B22222"][XmlIgnore]
        public List<Server> Servers
        {
            get { return this.servers; }
            set { this.servers = value; }
        }[/COLOR]

        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;
                }
            }
        }

        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);
                }
            }
        }

        // If you don't want to use "using" statements.
        public void WriteTo2(string FileName)
        {
            System.Xml.Serialization.XmlSerializerNamespaces namespaces = new System.Xml.Serialization.XmlSerializerNamespaces();
            namespaces.Add("", "");
            System.Xml.XmlTextWriter xmlTextWriter = new System.Xml.XmlTextWriter(FileName, Encoding.GetEncoding("ISO-8859-1"));
            System.Xml.Serialization.XmlSerializer xmlSerializer = new System.Xml.Serialization.XmlSerializer(this.GetType());
            xmlTextWriter.Formatting = System.Xml.Formatting.Indented;
            xmlTextWriter.Indentation = 2;
            xmlTextWriter.WriteStartDocument();
            xmlSerializer.Serialize((System.Xml.XmlWriter)xmlTextWriter, this, namespaces);
            xmlTextWriter.Close();
        }

        // Static method example
        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;
        }

        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);
                }
            }
        }
    }

    [XmlType]
    public class Server
    {
        private string name = string.Empty;

        public Server() { }

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

        [System.Xml.Serialization.XmlElement("name", IsNullable = false)]
        public string Name
        {
            get { return this.name; }
            set { this.name = value; }
        }
    }
}
psycik
Offline

Posting Freak

Posts: 5,210
Threads: 424
Joined: Sep 2005
#22
2011-11-24, 07:35 AM
funnily enough that's how I fixed it as well.
whurlston
Offline

Posting Freak

Posts: 7,885
Threads: 102
Joined: Nov 2006
#23
2011-11-25, 03:16 AM
To save you an hour of pain should you ever have a null list:
Code:
public Server[] ServersArray
        {
            get { return this.servers.ToArray(); }
            set { [color=#0000FF]if (value != null)[/color] this.servers = new List<Server>(value); }
        }
I was pulling my hair out trying to figure out why I could write the file but not read it.
« Next Oldest | Next Newest »

Users browsing this thread: 1 Guest(s)

Pages (3): « Previous 1 2 3


Possibly Related Threads…
Thread Author Replies Views Last Post
  Remote control example code? drmargarit 4 3,787 2018-04-21, 11:24 PM
Last Post: drmargarit
  Looking for C# UPnP Media Server code bgowland 5 7,645 2016-12-16, 08:25 PM
Last Post: mvallevand
  Problem with preview image for Pending Recordings Northpole 2 1,995 2012-08-14, 02:56 AM
Last Post: Northpole
  ActivatePopup: SSPlus problem ACTCMS 10 4,776 2012-07-18, 09:43 PM
Last Post: ACTCMS
  Resolving required plugin assemblies problem McBainUK 37 12,041 2011-10-26, 06:12 PM
Last Post: psycik
  sample video overlay plugin source code? reven 2 2,352 2011-10-03, 12:42 AM
Last Post: reven
  C# code to get data directory? McBainUK 2 2,790 2011-09-19, 07:57 PM
Last Post: McBainUK
  Source code for older versions ? Spark 1 1,756 2011-02-23, 01:19 AM
Last Post: sub
  Problem using UIStatic philcooling 13 4,393 2010-02-12, 06:34 PM
Last Post: whurlston
  special MVP code? scb147 31 8,439 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