when casting from a number the value must be a number less than infinity

mortias

New member
Joined
Aug 16, 2012
Messages
3
Programming Experience
10+
Hello, i'm new to C# and i'm playing around with serializing objects
as i understand the end result of that will be xml and i cant just "persist" objects like java
in a serialized state,

just to see what i'm doing wrong i'm posting this example which can't be saved and i don't see why
please help
probably it is very simple but i just want to know why

i get this exception on the serialize on the save method
"when casting from a number the value must be a number less than infinity"

tx

C#:
using System;
using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;
namespace FormsApp.com
{
    public class ProviderPool
    {
        public static readonly string Path = "Pool.osl";
        public Providers _pool;
        public ProviderPool()
        {
            Dump();
            AddProvider("Test1", 0.5618, 0.5698);
            AddProvider("Test2", 0.5618, 0.5698);
            AddProvider("Test3", 0.5618, 0.5698);
            Save();
            Load();
            Dump();
        }
        public void AddProvider(string name, double longitude, double latitude)
        {
            Providers._providers.Add(new Provider(name, longitude, latitude));
            Dump();
        }
        public void Dump()
        {
            foreach (Provider prv in Providers._providers)
            {
                Console.WriteLine(prv.Name + ", " + prv.Longitude + ", " + prv.Latitude);
            }
        }
        public void Load()
        {
            Stream stream = File.Open(Path, FileMode.Create);
            var s = new XmlSerializer(typeof (Providers));
            _pool = (Providers)s.Deserialize(stream);
            stream.Close();
        }
        public void Save()
        {
            Stream stream = File.Open(Path, FileMode.Create);
            var s = new XmlSerializer(typeof (Providers));
            s.Serialize(stream, Providers._providers);
            stream.Close();
        }
 
        #region Nested type: Provider
        public class Provider
        {
            [XmlAttribute(AttributeName = "Latitude")] public double Latitude;
            [XmlAttribute(AttributeName = "Longitude")] public double Longitude;
            [XmlAttribute(AttributeName = "Name")]
            public string Name;
            public Provider(string name, double longitude, double latitude)
            {
                Name = name;
                Longitude = longitude;
                Latitude = latitude;
            }
        }
        #endregion
        #region Nested type: Providers
        [XmlRoot("Pool")] 
        public class Providers
        {
            [XmlArray("Providers")]
            [XmlArrayItem("Provider", typeof(Provider), IsNullable = false)]
            public static List<Provider> _providers = new List<Provider>();
        }
        #endregion
    } 
}
 

JohnH

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
811
Location
Norway
Programming Experience
10+
I actually get this error:
Unable to cast object of type 'System.Collections.Generic.List`1[FormsApp.com.ProviderPool+Provider]' to type 'Providers'.
from this line
s.Serialize(stream, Providers._providers);
You can serialize that list, but the type of that is List<Provider>, not Providers. That would also render your Providers arrangements pointless.
If you want to serialize Providers use an object of this type, from what it looks like is what the _pool variable was intended. Then you must also remove the static declaration of the Providers list field, because static members does not belong to any object and is not serialized with the object.

There is also an error in Load, FileMode.Create will destroy the file before you get to read it.
as i understand the end result of that will be xml and i cant just "persist" objects like java
Binary serialization of objects exist with BinaryFormatter Class (System.Runtime.Serialization.Formatters.Binary)
 

mortias

New member
Joined
Aug 16, 2012
Messages
3
Programming Experience
10+
perfect,

if someone is interested here a slightly extended working example,
it should be better if you could serialize the IDictionary interface but ok..

C#:
using System;
using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;
namespace FormsApp.com
{
    public class ProviderPool
    {
        public readonly string Path = "Pool.osl";
        public Providers Pool = new Providers();
        public ProviderPool()
        {
            Dump();
            Pool.AddProvider("PRV1", "Test1", 0.5618, 0.5698);
            Pool.AddProvider("PRV2", "Test2", 0.5618, 0.5698);
            Pool.AddProvider("PRV3", "Test3", 0.5618, 0.5698);
            Save();
            Pool.RemovById("PRV2");
            Save();
            Load();
            Dump();
        }

        public void Dump()
        {
            Console.WriteLine("------------------------------------------");
            foreach (Provider prv in Pool.LProviders)
            {
                Console.WriteLine(prv.Name + ", " + prv.Longitude + ", " + prv.Latitude);
            }
        }
        public void Load()
        {
            Stream stream = File.Open(Path, FileMode.Open);
            var s = new XmlSerializer(typeof (Providers));
            Pool = (Providers) s.Deserialize(stream);
            stream.Close();
        }
        public void Save()
        {
            Stream stream = File.Open(Path, FileMode.Create);
            var s = new XmlSerializer(typeof (Providers));
            s.Serialize(stream, Pool);
            stream.Close();
        }
        #region Nested type: Provider
        public class Provider
        {
            [XmlAttribute(AttributeName = "Id")] public string Id;
            [XmlAttribute(AttributeName = "Latitude")] public double Latitude;
            [XmlAttribute(AttributeName = "Longitude")] public double Longitude;
            [XmlAttribute(AttributeName = "Name")] public string Name;
            public Provider()
            {
            }
            public Provider(string id, string name, double longitude, double latitude)
            {
                Id = id;
                Name = name;
                Longitude = longitude;
                Latitude = latitude;
            }
        }
        #endregion
        #region Nested type: Providers
        [XmlRoot("Pool")]
        public class Providers
        {
            [XmlArray("Providers")] [XmlArrayItem("Provider", typeof (Provider), IsNullable = false)] public
                List<Provider> LProviders = new List<Provider>();
            public void AddProvider(string id, string name, double longitude, double latitude)
            {
                LProviders.Add(new Provider(id, name, longitude, latitude));
            }
            public void RemovById(string id)
            {
                LProviders.RemoveAll(match: provider => provider.Id.Equals(id));
            }
        }
        #endregion
    }
}
 

mortias

New member
Joined
Aug 16, 2012
Messages
3
Programming Experience
10+
Implementing IXmlSerializable interface is an option for custom xml serialization.
nice, which gives me this xml below however when i want to bind it to
a datasource there are some issues i've found
"There are a couple of issues with Dictionary; the first is (as you've found) it doesn't implement the necessary IList/IListSource.
The second is that there is no guaranteed order to the items (and indeed, no indexer), making random access by index (rather than by key) impossible."

(now i'll try a binary persist)

C#:
  <?xml version="1.0"?>
<Pool xmlns:xsi="[URL]http://www.w3.org/2001/XMLSchema-instance[/URL]" xmlns:xsd="[url=http://www.w3.org/2001/XMLSchema]XML Schema[/url]">
  <LProviders>
    <item>
      <key>
        <string>PRV1</string>
      </key>
      <value>
        <Provider Latitude="0.5698" Longitude="0.5618" Name="Test1" />
      </value>
    </item>
    <item>
      <key>
        <string>PRV3</string>
      </key>
      <value>
        <Provider Latitude="0.5698" Longitude="0.5618" Name="Test3" />
      </value>
    </item>
  </LProviders>
</Pool>
 
Top Bottom