1

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

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

1 回答 1

3

使用序列化。

写入文件

var serializer = new JavaScriptSerializer();

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

并从文件中读取

var points = serializer.Deserialize<List<Point>>(File.ReadAllText(filename));
于 2013-06-22T22:24:04.560 回答