C#通过XML文件对数据进行读写操作
1.定义将要操作的数据结构
public class EditPoint
{
public double x { get; set; }
public double y { get; set; }
public int ID { get; set; }
public bool visible = false;
public EditPoint(int id, double x, double y, bool visible)
{
this.x = x;
this.y = y;
this.ID = id;
this.visible = visible;
}
}
public class EditEdge
{
public int ID { get; set; }
public int Src_ID { get; set; }
public int Dst_ID { get; set; }
public bool Directional = false;
public EditEdge(int id, int src_id, int dst_id, bool directional)
{
this.ID = id;
this.Src_ID = src_id;
this.Dst_ID = dst_id;
this.Directional = directional;
}
}
2.写操作
private void Save_XML()
{
try
{
//获取根节点对象
XDocument document = new XDocument();
XElement root = new XElement("map");
//设置根节点属性
root.SetAttributeValue("generator", "map editor");
root.SetAttributeValue("upload", "true");
root.SetAttributeValue("version", "0.6");
XElement vertex = new XElement("vertex");
vertex.SetAttributeValue("id", Global.editPoints[i].ID.ToString("0000"));
vertex.SetAttributeValue("visible", Global.editPoints[i].visible.ToString());
vertex.SetAttributeValue("x", Global.editPoints[i].x.ToString("0.0000000"));
vertex.SetAttributeValue("y", Global.editPoints[i].y.ToString("0.0000000"));
root.Add(vertex);
XElement edge = new XElement("edge");
edge.SetAttributeValue("id", Global.editEdges[j].ID.ToString("0000"));
edge.SetAttributeValue("src_id", Global.editEdges[j].Src_ID.ToString("0000"));
edge.SetAttributeValue("dst_id", Global.editEdges[j].Dst_ID.ToString("0000"));
edge.SetAttributeValue("directional", Global.editEdges[j].Directional.ToString());
root.Add(edge);
System.Windows.Forms.SaveFileDialog sfd = new System.Windows.Forms.SaveFileDialog();
sfd.Filter = "地图数据文件(*.xml)|*.xml|所有文件|*.*";//设置文件类型
sfd.FileName = "road_map";//设置默认文件名
sfd.DefaultExt = "xml";//设置默认格式(可以不设)
sfd.AddExtension = true;//设置自动在文件名中添加扩展名
//设置保存路径
if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
root.Save(sfd.FileName);
}
else
{
root.Save(System.Environment.CurrentDirectory+ @"\road_map.xml");
}
catch (Exception)
{
return;
}
}
3.读操作
private void Read_XML()
{
string szXMLFullPathName = System.Environment.CurrentDirectory+ @"\road_map.xml";
if (File.Exists(szXMLFullPathName))
{
载入路网数据
//将XML文件加载进来
XDocument document = XDocument.Load(szXMLFullPathName);
//获取到XML的根元素进行操作
XElement root = document.Root;
XElement vertex = new XElement("vertex");
XElement edge = new XElement("edge");
//获取根元素下的所有子元素
IEnumerable<XElement> enumerable = root.Elements();
foreach (XElement item in enumerable)
{
if (item.Name == "vertex")
{
EditPoint editPoint = new EditPoint(int.Parse(item.Attribute("id").Value), double.Parse(item.Attribute("x").Value), double.Parse(item.Attribute("y").Value), bool.Parse(item.Attribute("visible").Value));
}
else if (item.Name == "edge")
{
EditEdge editEdge = new EditEdge(int.Parse(item.Attribute("id").Value), int.Parse(item.Attribute("src_id").Value), int.Parse(item.Attribute("dst_id").Value), bool.Parse(item.Attribute("directional").Value));
}
}
}