XML文件基本概念

可扩展标记语言(英语:Extensible Markup Language,简称:XML),是一种标记语言。标记指计算机所能理解的信息符号,通过此种标记,计算机之间可以处理包含各种信息的文章等。如何定义这些标记,既可以选择国际通用的标记语言,比如HTML,也可以使用像XML这样由相关人士自由决定的标记语言,这就是语言的可扩展性。XML是从标准通用标记语言(SGML)中简化修改出来的,可以在不同语言或者平台进行数据传输。

基本要素

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?xml version="1.0" encoding="utf-8"?>  <!--  文档声明   -->
<students> <!-- 元素=标签 students为根标签 ,或者称之为根元素,程序处理时称之为节点-->
<student name="xiaoming"></student>
<student>
<name>xaiohong</name>
<!--特殊字符-->
<read>&lt;三个火枪手&gt;</read>
</student>
<student>
<name>xiaoming</name>
</student>
<!--![CDATA[不想解析的内容,浏览器会直接输出内容]]>-->
<![CDATA[
<student>
<name>xaiodong</name>
</student>
]]>

</students>

内容预览:

C#读写XML文档

引用命名空间
using System.Xml;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/// <summary>
/// 写入XML端口号默认index
/// </summary>
/// <param name="COMIndex">端口号Index</param>
public static void WriteXML(string COMIndex)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(@"SysConfig.xml");
//获取根节点
XmlNode node = xmlDoc.SelectSingleN("configuration");
XmlNodeList nodeList = node.ChildNodes;
foreach (XmlNode xn in nodeList)
{
XmlNodeList childList =xn.ChildNodes;
foreach (XmlNode node1 in childList)
{
node1.InnerText = COMIndex;//修改端口号配置
break;
}
break;
}
xmlDoc.Save("SysConfig.xml");
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
using System.Data;
using System.IO;

namespace Serialport.Operate
{
class ReadSysConfig
{
private static string _strcomIndex = string.Empty;
private static string _strchannelColor = string.Empty;
private static string _strtrendTime = string.Empty;

private static string _strmaxAmplitude = string.Empty;
private static string _strminAmplitude = string.Empty;
private static string _strinterval = string.Empty;

public static void LoadConfig()
{
DataTable m_dtSysConfig = new DataTable();
//加载系统配置参数
m_dtSysConfig.Columns.Add(FileNameSysConfig.comIndex, typeof(string));
m_dtSysConfig.Columns.Add(FileNameSysConfig.channelColor, typeof(string));
m_dtSysConfig.Columns.Add(FileNameSysConfig.trendTime, typeof(string));
m_dtSysConfig.Columns.Add(FileNameSysConfig.maxAmplitude, typeof(string));
m_dtSysConfig.Columns.Add(FileNameSysConfig.minAmplitude, typeof(string));
NewMethod(m_dtSysConfig);
m_dtSysConfig.TableName = "Sysconfig";
if (File.Exists(FilePathSysConfig.configXmlPath))
{
m_dtSysConfig.ReadXml(FilePathSysConfig.configXmlPath);

_strcomIndex = m_dtSysConfig.Rows[0][FileNameSysConfig.comIndex].ToString().Trim();
_strchannelColor = m_dtSysConfig.Rows[0][FileNameSysConfig.channelColor].ToString().Trim();
_strtrendTime = m_dtSysConfig.Rows[0][FileNameSysConfig.trendTime].ToString().Trim();
_strmaxAmplitude = m_dtSysConfig.Rows[0][FileNameSysConfig.maxAmplitude].ToString().Trim();
_strminAmplitude = m_dtSysConfig.Rows[0][FileNameSysConfig.minAmplitude].ToString().Trim();
_strinterval = m_dtSysConfig.Rows[0][FileNameSysConfig.interval].ToString().Trim();
}
}

private static void NewMethod(DataTable m_dtSysConfig)
{
m_dtSysConfig.Columns.Add(FileNameSysConfig.interval, typeof(string));
}

public static string StrcomIndex { get => _strcomIndex; set => _strcomIndex = value; }
public static string StrchannelColor { get => _strchannelColor; set => _strchannelColor = value; }
public static string StrtrendTime { get => _strtrendTime; set => _strtrendTime = value; }
public static string StrmaxAmplitude { get => _strmaxAmplitude; set => _strmaxAmplitude = value; }
public static string StrminAmplitude { get => _strminAmplitude; set => _strminAmplitude = value; }
public static string Strinterval { get => _strinterval; set => _strinterval = value; }
}

class FileNameSysConfig//初始化
{
public static readonly string comIndex = "comIndex";
public static readonly string channelColor = "channelColor";
public static readonly string trendTime = "trendTime";
public static readonly string minAmplitude = "minAmplitude";
public static readonly string maxAmplitude = "maxAmplitude";
public static readonly string interval = "interval";
}
class FilePathSysConfig
{
public static readonly string configXmlPath = @"SysConfig.xml";
}
}

写在后面

读写内容写的比较简单,因为目前还没有涉及太复杂的东西,后续有其他内容需要用到会一并在这篇文章中更新😅。