1.XML帮助类
public class XmlHelper { public static string GetElementByName(string xmlFileName, string tagName) { try { string result = null; XmlDocument doc = new XmlDocument(); doc.Load(xmlFileName); XmlNodeList nodes = doc.GetElementsByTagName(tagName); foreach (XmlNode v in nodes) { result = v.InnerText; } return result; } catch { throw new Exception("打开文件错误"); } }
2.应用
SerialPort sport = new SerialPort(); if (sport.IsOpen == false) { string appPath = AppDomain.CurrentDomain.BaseDirectory; string sIniFile = appPath + "wmsconfig" + "\\" + "CardSetting.xml"; if (File.Exists(sIniFile)) { sport.BaudRate = Convert.ToInt32(XmlHelper.GetElementByName(sIniFile, "BaudRate")); sport.DataBits = Convert.ToInt32(XmlHelper.GetElementByName(sIniFile, "DataBits")); sport.StopBits = (StopBits)Convert.ToInt32(XmlHelper.GetElementByName(sIniFile, "StopBits")); sport.Parity = (Parity)Convert.ToInt32(XmlHelper.GetElementByName(sIniFile, "Parity")); var portName = XmlHelper.GetElementByName(sIniFile, "PortName"); if (null != portName) sport.PortName = portName; } sport.BaudRate = 9600; sport.DataBits = 8; sport.StopBits = StopBits.One; sport.Parity = Parity.None; try { sport.Open();//打开串口 sport.DtrEnable = true;//设置DTR为高电平 sport.RtsEnable = true;//设置RTS位高电平 } catch (Exception ex) { //MessageBox.Show(ex.Message); }
标签:xml,文件,读取,XmlHelper,GetElementByName,StopBits,sport,sIniFile,string From: https://www.cnblogs.com/king10086/p/16742133.html