C# XML文件操作之相机参数保存和读取

  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

  {

  ///

  /// 创建相机XML文件

  ///

  /// 存储路径

  /// 相机列表

  public static void GenerateCameraXMLFile(string xmlFilePath, List 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 cameraList)

  {

  cameraList = new List();

  //初始化一个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());

  }

  }

  }

  }