If you wish to save one Point to file, you could remove foreach completely and do saving with "file.add(this.GetXml())".
But there is no sense in saving only one point. You will probably have some collections of Points (maybe Route or CircularQueue as in original). So you should add Save method to Route, not to Point. If your Route class implements IEnumerable - you can do "foreach(Point p in this)". Alternatively if Route class contains, for example "List<Point> Points" you could do "foreach(Point p in Points)".
Also, if you have different routes, you aren't forced to save one route per file.
Here is my Route class:
Code:
public class Route
{
public string From;
public string To;
public string Name;
public List<WayPoint> Points;
public Route(string name, string from, string to)
{
Points = new List<WayPoint>();
Name = name;
From = from;
To = to;
}
public Route(string name)
: this(name, null, null)
{
}
public Route()
{
}
public Route(XElement xml) : this(
xml.Attribute("Name").Value,
xml.Attribute("From").Value,
xml.Attribute("To").Value)
{
foreach (XElement xpoint in xml.Descendants("Point"))
Points.Add(new WayPoint(xpoint));
}
public XElement GetXml()
{
XElement xroute = new XElement("Route",
new XAttribute("Name", Name),
new XAttribute("From", From),
new XAttribute("To", To));
foreach (WayPoint point in Points)
xroute.Add(point.GetXml());
return xroute;
}
}
fragments of RouteManager:
Code:
public List<Route> Routes;
public RouteManager()
{
Routes = new List<Route>();
}
public RouteManager(string filePath)
: this()
{
if (!File.Exists(filePath))
{
throw new FileNotFoundException("Could not find the specified file!", filePath);
}
XElement file = XElement.Load(filePath);
IEnumerable<XElement> xroutes = file.Descendants("Route");
foreach (XElement xroute in xroutes)
{
Route route = new Route(xroute);
Routes.Add(route);
}
}
public void Save(string filePath)
{
XElement file = new XElement("RoutesManager");
foreach (Route route in Routes)
file.Add(route.GetXml());
file.Save(filePath);
}
Originally Posted by
Fabolous1
I'm getting an error adding burdakovd's save waypoint method to the point class:
foreach statement cannot operate on variables of type 'BotTut_Nav_Waypoints.Point' because 'BotTut_Nav_Waypoints.Point' does not contain a public definition for 'GetEnumerator'
I've been messing around with it some and can't seem to get it to work.
I'm still trying to wrap my head around implementing this waypoint system, any help is appreciated. Thanks a lot.
The code I'm refering to is this: