【C#】读XML文件
1.xml文件格式
<?xml version="1.0" encoding="utf-8" ?>
<root>
<parameter>
<lasertype>2</lasertype>
</parameter>
</root>
2.C#实现读取XML文件功能
public static void LoadXml()
{
try
{
//判别文件是否存在,存在才读取
if (File.Exists(path))
{
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(path);
XmlNodeList xnl = xmlDocument.SelectSingleNode("root").ChildNodes;
foreach (XmlNode xn in xnl)
{
if (xn.Name == "parameter")
{
XmlNodeList xnl1 = xn.ChildNodes;
foreach (XmlNode node in xnl1)
{
if (node.Name == "lasertype")
{
GlobalValue.LaserType = Convert.ToInt16(node.InnerText);
}
}
}
}
}
}
catch (Exception)
{
throw;
}
}