点采集操作消息体封装与解译1

项目:基于GIS操作语义的制图系统

需求:将点采集操作封装,写入和读取XML

1 xml结构设计

<?xml version="1.0" ecoding="UTF-8">
<Project ID="">
	<CooperationMap ID="">
		<Layer Name="" Type="">
			<Point Operation=""></Point>
		</Layer>
	</CooperationMap>
</Project>
<工程 ID=“”>
	<协作区域 ID="”>
		<图层 名称="" 类型=“点”>
			<点 操作类型="采集">坐标信息<点>
		</图层>
	</协作区域ID>
</工程ID>

2 消息体封装

public enum OperationEnum
{
    Add,
    Remove
}
class OperationInformation
{
    public string Project { get; set; }
    public string CooperationMap { get; set; }
    public string LayerName { get; set; }
    public string LayerType { get; set; }
    public OperationEnum OperationEnum { get; set; }
    public IPointCollection PointCollection;
}

几何(Geometry)属性用点集(IPointCollection)存储。

3 读写xml

写入xml

public void WriteXML(string absolutePath)
{
    IPoint point = PointCollection.get_Point(0);
    //创建节点
    XElement project = new XElement("Project",
                                    new XAttribute("ID", this.Project));
    XElement cooperationMap = new XElement("CooperationMap",
                                           new XAttribute("ID", this.CooperationMap));
    XElement layer = new XElement("Layer",
                                  new XAttribute("Name", this.LayerName),
                                  new XAttribute("Type", this.LayerType));
    XElement geometry = new XElement("Point",
                                     new XAttribute("Operation", this.OperationEnum),
                                     point.X," ", point.Y);
    //添加节点
    project.Add(cooperationMap);
    cooperationMap.Add(layer);
    layer.Add(geometry);
    //保存
    project.Save(absolutePath);
}

读取xml

public void ReadXML(string absolutePath)
{
    //加载文档
    XDocument document = XDocument.Load(absolutePath);
    //获取节点
    XElement project = document.Root;
    XElement cooperationMap = project.Element("CooperationMap");
    XElement layer = cooperationMap.Element("Layer");
    XElement geometry = layer.Element("Point");
    //获取参数
    this.Project = project.Attribute("ID").Value;
    this.CooperationMap = cooperationMap.Attribute("ID").Value;
    this.LayerName = layer.Attribute("Name").Value;
    this.LayerType = layer.Attribute("Type").Value;
    //字符串转枚举
    this.OperationEnum = (OperationEnum)Enum.Parse(typeof(OperationEnum), geometry.Attribute("Operation").Value);
    string[] sPoint = geometry.Value.Split(' ');
    IPoint point = new PointClass();
    point.X = double.Parse(sPoint[0]);
    point.Y = double.Parse(sPoint[1]);
    this.PointCollection = new PolylineClass();
    this.PointCollection.AddPoint(point);
}

引用

private void m_EngineEditEvent_OnSketchFinished()
{
    string relatePath = Application.StartupPath + @"......Data789.xml";
    string absolutePath = System.IO.Path.GetFullPath(relatePath);
    OperationInformation operationInformation = new OperationInformation()
    {
        Project = "123",
        CooperationMap = "123",
        LayerName = axMapControl1.get_Layer(0).Name,
        LayerType = "Point",
        OperationEnum = OperationEnum.Add,
    };
    operationInformation.PointCollection = new PolygonClass();
    operationInformation.PointCollection.AddPoint(point);
    operationInformation.WriteXML(absolutePath);
}

private void 读取xmlToolStripMenuItem_Click(object sender, EventArgs e)
{
    OperationInformation operationInformation = new OperationInformation();
    string relatePath = Application.StartupPath + @"......Data789.xml";
    string absolutePath = System.IO.Path.GetFullPath(relatePath);
    operationInformation.ReadXML(absolutePath);
}

4 参考资料

C#读取和写入XML文件

在 C# 中创建 XML 树 (LINQ to XML)

C# 字符串(string) 转成 枚举(enum)类型

原文地址:https://www.cnblogs.com/liuwenzhen/p/12752470.html