xml文件:
<ModbusConfig> <Module Id="0001" Name="1#变频器" Description="1#变频器"> <DataPoint Id="00001" StartingRegister="2000" DataType="U16" DataPointType="0" Name="1#变频器启动/1#变频器停机" Description="数字量点" /> <DataPoint Id="00002" StartingRegister="2001" DataType="U16" DataPointType="0" Name="1#变频器报警" Description="数字量点" /> <DataPoint Id="00003" StartingRegister="0010" DataType="U16" DataPointType="0" Name="变频器输出频率" Description="监控参数" /> <DataPoint Id="00004" StartingRegister="0011" DataType="U16" DataPointType="0" Name="变频器输出电流" Description="监控参数" /> <DataPoint Id="00005" StartingRegister="0012" DataType="U16" DataPointType="0" Name="输出转速" Description="监控参数" /> <DataPoint Id="00006" StartingRegister="0013" DataType="U16" DataPointType="0" Name="输出力矩" Description="监控参数" /> <DataPoint Id="00007" StartingRegister="0014" DataType="U16" DataPointType="0" Name="电机功率" Description="监控参数" /> <DataPoint Id="00008" StartingRegister="0015" DataType="U32" DataPointType="0" Name="电机运行时间" Description="监控参数" /> <DataPoint Id="00009" StartingRegister="0017" DataType="U16" DataPointType="0" Name="出气口压力" Description="监控参数" /> </Module> <Module Name="2#变频器" Description="2#变频器" Id="0002"> <DataPoint Id="00010" StartingRegister="2002" DataType="U16" DataPointType="0" Name="2#变频器启动/2#变频器停机" Description="数字量点" /> <DataPoint Id="00010" StartingRegister="2002" DataType="U16" DataPointType="0" Name="2#变频器启动/2#变频器停机" Description="数字量点" />
</Module> </ModbusConfig>
读写:
private void ReadConfigFile() { string modbusConfigRelativePath = ApplicationSettingsFactory.GetApplicationSettings().ModbusConfigFilePath; string modbusConfigFile = System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, modbusConfigRelativePath); if (System.IO.File.Exists(modbusConfigFile)) { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(modbusConfigFile); XmlNodeList moduleNodes = xmlDoc.SelectNodes("/ModbusConfig/Module"); if (null != moduleNodes && moduleNodes.Count > 0) { ClearDictionary(); int moduleId = 0; int dataPointId = 0; foreach (XmlNode moduleNode in moduleNodes) { // Module moduleId++; XmlElement moduleElement = (XmlElement) moduleNode; string moduleNumber = Convert.ToString(moduleId); moduleElement.SetAttribute("Id", string.Format("{0:D4}", moduleId)); string moduleName = moduleElement.GetAttribute("Name"); string moduleDescription = moduleElement.GetAttribute("Description"); Module module = new Module(); //UnitOfWork机制和数据库外键的设计,决定了要预先设置数据库中的主键Id //尽管数据库自己生产主键。 module.Id = moduleId; AddModule(module, moduleNumber, moduleName, moduleDescription); //DataPoint XmlNodeList dataPointNodes = moduleNode.ChildNodes; if (dataPointNodes.Count > 0) { foreach (XmlNode dataPointNode in dataPointNodes) { dataPointId++; XmlElement dataPointElement = (XmlElement) dataPointNode; string dataPointNumber = Convert.ToString(dataPointId); dataPointElement.SetAttribute("Id", string.Format("{0:D5}", dataPointId)); string dataPointName = dataPointElement.GetAttribute("Name"); string dataPointStartingRegister = dataPointElement.GetAttribute("StartingRegister"); string dataPointDataType = dataPointElement.GetAttribute("DataType"); string dataDataDataPointType = dataPointElement.GetAttribute("DataPointType"); string datapointDescription = dataPointElement.GetAttribute("Description"); DataPoint dataPoint = new DataPoint(); //UnitOfWork机制和数据库外键的设计,决定了要预先设置数据库中的主键Id //尽管数据库自己生产主键。 dataPoint.Id = dataPointId; AddDataPoint(module, dataPoint, dataPointNumber, dataPointStartingRegister, dataPointName, dataPointDataType, dataDataDataPointType, datapointDescription); } } } xmlDoc.Save(modbusConfigFile); } } }