C# XML文件操作之相机参数保存和读取
作者:MechMaster
XML是可扩展标记语言是一种数据语言,它将数据以一种简单的文本格式存储,可以被人类和几乎任何计算机理解。本文将利用C#实现相机参数读取并保存至XML文件,感兴趣的可以学习一下
1. XML基础
XML是可扩展标记语言(Extensible Markup Language,XML)是一种数据语言,它将数据以一种简单的文本格式存储,可以被人类和几乎任何计算机理解。Microsoft在.Net Framework和其他微软产品中已经完全采用它。
XML的基本格式:
<book> <title>Beginning Visual C# 7</title> <author>Benjamin Perkins et al</author> <code>458685</code> </book>
2. XML基本操作
3. 相机参数保存和读取
创建一个相机参数XML文件保存和读取的类CameraXML
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Xml; namespace xmlTest { class Camera { public string camID; public int camExposure; public int camGain; public Camera() { camID = "nullID"; camExposure = 5000; camGain = 0; } public Camera(string camID, int camExposure, int camGain) { this.camID = camID; this.camExposure = camExposure; this.camGain = camGain; } } class CameraXML { /// <summary> /// 创建相机XML文件 /// </summary> /// <param name="xmlFilePath">存储路径</param> /// <param name="cameraList">相机列表</param> public static void GenerateCameraXMLFile(string xmlFilePath, List<Camera> cameraList) { try { //初始化一个xml实例 XmlDocument myXmlDoc = new XmlDocument(); //创建xml的根节点 XmlElement rootElement = myXmlDoc.CreateElement("Cameras"); //将根节点加入到xml文件中(AppendChild) myXmlDoc.AppendChild(rootElement); foreach (var item in cameraList) { //初始化第一层的第一个子节点 XmlElement firstLevelElement1 = myXmlDoc.CreateElement("Camera"); //填充第一层的第一个子节点的属性值(SetAttribute) firstLevelElement1.SetAttribute("ID", item.camID); //将第一层的第一个子节点加入到根节点下 rootElement.AppendChild(firstLevelElement1); //初始化第二层的第一个子节点 XmlElement secondLevelElement11 = myXmlDoc.CreateElement("Exposure"); //填充第二层的第一个子节点的值(InnerText) secondLevelElement11.InnerText = item.camExposure.ToString(); firstLevelElement1.AppendChild(secondLevelElement11); XmlElement secondLevelElement12 = myXmlDoc.CreateElement("Gain"); secondLevelElement12.InnerText = item.camGain.ToString(); firstLevelElement1.AppendChild(secondLevelElement12); } //将xml文件保存到指定的路径下 myXmlDoc.Save(xmlFilePath); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } } public static void ReadCameraXMLFile(string xmlFilePath, out List<Camera> cameraList) { cameraList = new List<Camera>(); //初始化一个xml实例 XmlDocument myXmlDoc = new XmlDocument(); //加载xml文件(参数为xml文件的路径) myXmlDoc.Load(xmlFilePath); //获得第一个匹配的节点(SelectSingleNode):此xml文件的根节点 XmlNode rootNode = myXmlDoc.SelectSingleNode("Cameras"); //分别获得该节点的InnerXml和OuterXml信息 string innerXmlInfo = rootNode.InnerXml.ToString(); string outerXmlInfo = rootNode.OuterXml.ToString(); //获得该节点的子节点(即:该节点的第一层子节点) XmlNodeList firstLevelNodeList = rootNode.ChildNodes; try { foreach (XmlNode node in firstLevelNodeList) { Camera camera = new Camera(); //获得该节点的属性集合 XmlAttributeCollection attributeCol = node.Attributes; foreach (XmlAttribute attri in attributeCol) { //获取属性名称与属性值 string name = attri.Name; string value = attri.Value; camera.camID = value;//相机ID } //判断此节点是否还有子节点 if (node.HasChildNodes) { //获取该节点的第一个子节点 XmlNode secondLevelNode1 = node.FirstChild; //获取该节点的名字 string name = secondLevelNode1.Name; //获取该节点的值(即:InnerText) string innerText = secondLevelNode1.InnerText; camera.camExposure = Convert.ToInt16(innerText);//相机曝光 //获取该节点的第二个子节点(用数组下标获取) XmlNode secondLevelNode2 = node.ChildNodes[1]; name = secondLevelNode2.Name; innerText = secondLevelNode2.InnerText; camera.camGain = Convert.ToInt16(innerText);//相机增益 } cameraList.Add(camera); } } catch (Exception ex) { Console.WriteLine(ex.ToString()); } } } }
在winform中使用这个类
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Xml;//引入命名空间 namespace xmlTest { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void buttonGenerateCameraXMLFile_Click(object sender, EventArgs e) { List<Camera> cameraList = new List<Camera>(); Camera camera; camera = new Camera("L102314123", 4500, 0); cameraList.Add(camera); camera = new Camera("L102314124", 5500, 0); cameraList.Add(camera); camera = new Camera("L102314125", 3500, 1); cameraList.Add(camera); camera = new Camera("L102314126", 6500, 0); cameraList.Add(camera); camera = new Camera("L102314127", 4880, 0); cameraList.Add(camera); camera = new Camera("L102314128", 9500, 0); cameraList.Add(camera); camera = new Camera("L102314129", 4530, 0); cameraList.Add(camera); camera = new Camera("L102314130", 13200, 0); cameraList.Add(camera); CameraXML.GenerateCameraXMLFile("D:\\Cameras.xml", cameraList); } private void buttonReadCameraXMLFile_Click(object sender, EventArgs e) { List<Camera> cameraList = new List<Camera>(); CameraXML.ReadCameraXMLFile("D:\\Cameras.xml", out cameraList); foreach (var item in cameraList) { listBox1.Items.Add("ID:" + item.camID); listBox1.Items.Add("Exposure:" + item.camExposure); listBox1.Items.Add("Gain:" + item.camGain); } } } }
到此这篇关于C# XML文件操作之相机参数保存和读取的文章就介绍到这了,更多相关C#相机参数保存 读取内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!