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