一、简介
1、传统的配置文件ini已有被xml文件逐步代替的趋势,这里主要讨论XML配置文件。
2、需要引入using System.Xml.Linq;命名空间
二、ini网址
https://www.cnblogs.com/cncc/p/3415694.html(重点)
https://www.cnblogs.com/xmy-007/p/6400221.html
https://www.cnblogs.com/CUIT-DX037/p/6797271.html
http://www.cnblogs.com/NoticeWang/p/3267865.html
三、XML配置文件
下面给定的一个例子,就能满足你配置文件的需求。注意这个配置文件是独立的,它是在我的工程中没有添加上去的。它只是存在我设定的特定目录中。
1、放在工作目录下
2、xml的内容:
standalone="yes" 这段代码必须要有,才能保证文件的独立,即你不用在工程里面添加IPconfig.xml文件,你也可以访问它。
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<library id="30">
<IP id="20">
<IPValue>192.168.6.2</IPValue>
</IP>
</library>
3、工程里面不用添加IPconfig.xml文件:在主程序添加即可。
4、参考博客:
https://www.cnblogs.com/fengxuehuanlin/p/5631664.html
https://www.cnblogs.com/xuxiaoshuan/p/5752434.html
四、XML应用举例
参考:
https://zhidao.baidu.com/question/588701587.html
https://www.cnblogs.com/joean/p/4985348.html
4.1读XML
4.1.1、ExposeStatusTimeconfig.XML文件
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<ExposeStatusTimeconfig>
<ExposeReady>
<ExposeReadyValue>1000</ExposeReadyValue>
</ExposeReady>
<ExposeOperating>
<ExposeOperatingValue>3000</ExposeOperatingValue>
</ExposeOperating>
</ExposeStatusTimeconfig>
4.1.2、调用ExposeStatusTimeconfig.XML文件的函数(C#工程请自己建)
public void threadExposeFuuc()
{
#region 曝光图标显示时间的配置文件
string strExposeStatusTimeconfigPath;
string strExposeReady = null;
string strExposeOperating = null;
strExposeStatusTimeconfigPath = Environment.CurrentDirectory + "\\ExposeStatusTimeconfig.xml";
XDocument document = XDocument.Load(strExposeStatusTimeconfigPath);
//获取到XML的根元素进行操作
XElement root = document.Root;//根节点(即第一个节点)
//配置绿色图标
XElement ExposeReady = root.Element("ExposeReady");//获取根节点的第二个节点
XElement ExposeReadyValue = ExposeReady.Element("ExposeReadyValue");//获取该节点的元素,即第三个节点
strExposeReady = ExposeReadyValue.Value;
//配置黄色图标
XElement ExposeOperating = root.Element("ExposeOperating");//获取根节点的第二个节点
XElement ExposeOperatingValue = ExposeOperating.Element("ExposeOperatingValue");//获取该节点的元素,即第三个节点
strExposeOperating = ExposeOperatingValue.Value;
#endregion
int iExposeReady = 0;
int iExposeOperating = 0;
iExposeReady = Convert.ToInt32(strExposeReady);
iExposeOperating = Convert.ToInt32(strExposeOperating);
m_cGlobalStrct.m_cLogs.WriteDebugLog("TimerBoard::TimerSerialCallBackFunProc-- 曝光绿色图标的显示时间 = " + strExposeReady);
m_cGlobalStrct.m_cLogs.WriteDebugLog("TimerBoard::TimerSerialCallBackFunProc-- 曝光黄色图标的显示时间 = " + strExposeOperating);
//绿色图标
m_cGlobalStrct.m_cLogs.WriteDebugLog("TimerBoard::TimerSerialCallBackFunProc-- 曝光准备好");
m_cGlobalStrct.m_cStatusBarProperty.ExpStatusVal = "曝光准备好";
m_cGlobalStrct.m_cStatusBarProperty.ExpIndicateIcon = @"..\Icon\StatusBar\预曝光.png";
Thread.Sleep(iExposeReady);
//黄色图标
m_cGlobalStrct.m_cLogs.WriteDebugLog("TimerBoard::TimerSerialCallBackFunProc-- 正在曝光");
m_cGlobalStrct.m_cStatusBarProperty.ExpStatusVal = "正在曝光";
m_cGlobalStrct.m_cStatusBarProperty.ExpIndicateIcon = @"..\Icon\StatusBar\正在曝光.png";
Thread.Sleep(iExposeOperating);
}
4.2、写XML
4.2.1、SocketAndBullHeadConfig.XML
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<library>
<Socket>
<SocketStart>1</SocketStart>
<SocketPort>48888</SocketPort>
<SocketIP>127.0.0.1</SocketIP>
</Socket>
<BullHead>
<BullHeadStart>1</BullHeadStart>
<COM>COM3</COM>
<BaudRate>38400</BaudRate>
</BullHead>
<Http>
<HttpStart>1</HttpStart>
<HttpPort>8001</HttpPort>
</Http>
</library>
4.2.2工程
using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace QueueSample
{
/***********触发***********/
class Program
{
static void Main(string[] args)
{
//记录该波特率值到配置文件中
string strCOMconfigFilePath = @"D:\keenray6.2\trunk\Bin\Config" + "\\SocketAndBullHeadConfig.xml";
XDocument document = XDocument.Load(strCOMconfigFilePath);
//获取到XML的根元素进行操作
XElement root = document.Root;//根节点
XElement BullHeadRoot = root.Element("BullHead"); //第二级节点
//BullHeadRoot.SetAttributeValue("BaudRate", 9600);
XElement BullHeadBaudRate = BullHeadRoot.Element("BaudRate"); //波特率
//BullHeadBaudRate.SetElementValue("BaudRate",9600);//设置子元素的值
//BullHeadBaudRate.SetAttributeValue("AAAA", 121212);//设置元素属性的值
BullHeadBaudRate.SetValue(38400);
document.Save(strCOMconfigFilePath);
Console.ReadLine();
}
}
}
五、总结
关于XML的语法说明及其详细的应用,在往后的博客中给出。