zoukankan      html  css  js  c++  java
  • Xml文件读写

    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);
                    }
                }
            }
  • 相关阅读:
    nginx高级玩法之根据来源ip分流
    ubuntu上的 /dev/loop0 到 /dev/loop18占到100%的处理
    nginx: [warn] conflicting server name "aaa.bbbb.com" on 0.0.0.0:80, ignored
    nginx报警:nginx: [warn] could not build optimal server_names_hash, you should increase either server_names_hash_max_size: 512 or server_names_hash_bucket_size: 64; ignoring server_names_hash_bucket_size
    nginx配置socket连接
    Syntax error: "(" unexpected shell里面的报错解决
    docker批量操作容器
    ubuntu18.04安装docker和开通对外2375端口(方便portainer管理)
    Cadence OrCad Allegro SPB 16.6 下载及安装破解指南
    关于XILINX芯片IO管脚的上拉电阻的疑问
  • 原文地址:https://www.cnblogs.com/easy5weikai/p/3235894.html
Copyright © 2011-2022 走看看