写IEnumerable <ienumerable <point>&gt;提交</ienumerable <point>

时间:2013-06-22 22:18:04

标签: c# io ienumerable

我无法弄清楚如何将此集合写入文件。我有以下课程

public static class GeoPolyLines
{
    public static ObservableCollection<Connections> connections = new ObservableCollection<Connections>(); 
}

public class Connections
{
    public IEnumerable<IEnumerable<Point>> Points { get; set; }

    public Connections(Point p1, Point p2)
    {
        Points = new List<List<Point>>
         {
                new List<Point>
                {
                    p1, p2
                }
         };
    }

}

然后是一堆这样的事情:

 GeoPolyLines.connections.Add(new Connections(new Point(GeoLocations.locations[0].Longitude, GeoLocations.locations[0].Latitude), new Point(GeoLocations.locations[1].Longitude, GeoLocations.locations[1].Latitude)));

所以GeoPolyLines.connections最终会有一堆不同的位置,我想写一个.txt文件,以便在需要时保存并重新加载。但我不知道该怎么做。我有这样的事情:

using (StreamWriter sw = new StreamWriter(filename))
{
    var enumerator = GeoPolyLines.connections.GetEnumerator();

    while (enumerator.MoveNext())
    {

    }
    sw.Close();
 }

1 个答案:

答案 0 :(得分:3)

使用序列化。

写入文件

var serializer = new JavaScriptSerializer();

File.WriteAllText(filename, serializer.Serialize(points));

并从文件中读取

var points = serializer.Deserialize<List<Point>>(File.ReadAllText(filename));