//需要安装 Microsoft.Extensions.Configuration;
//Microsoft.Extensions.Configuration.json
//Microsoft.Extensions.Configuration.xml
//Microsoft.Extensions.Configuration.Binder;
//注意: json 的key中不能出现冒号":"
//XML的 Atrribute中不能出现关键词 “Name”, 且不能使用根节点(从第一个子节点开始)
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//需要安装 Microsoft.Extensions.Configuration;
//Microsoft.Extensions.Configuration.json
//Microsoft.Extensions.Configuration.xml
//Microsoft.Extensions.Configuration.Binder;
//注意: json 的key中不能出现冒号":"
//XML的 Atrribute中不能出现关键词 "Name", 且不能使用根节点(从第一个子节点开始)
namespace XMLFrameworkDemon
{
internal class Program
{
static void Main(string[] args)
{
ConfigurationBuilder cfgbuilder = new ConfigurationBuilder();
cfgbuilder.AddXmlFile("cfg.xml");
IConfigurationRoot root = cfgbuilder.Build(); ;
string name = root.GetSection("NodeClass:ModbusNode:ModbusGroup:NameXML").Value;
//string variable = root.GetSection("NodeClass:ModbusNode:Variable:0:NameXML").Value; //不行
ConfigurationBuilder cfgJsonbuilder = new ConfigurationBuilder();
cfgJsonbuilder.AddJsonFile("VariableNode.json");
IConfigurationRoot rootJson = cfgJsonbuilder.Build();
string nameJson = rootJson.GetSection("NodeClass:ModbusNode:ModbusGroup:Name").Value;
Console.WriteLine("=======XML===========");
Console.WriteLine(name);
Console.WriteLine("========Json==========");
Console.WriteLine(nameJson);
Console.WriteLine(" ");
Console.WriteLine("=======使用extensions.Configuration.Binder获取变量===========");
Console.WriteLine(" ");
Variable variablexml = root.GetSection("NodeClass:ModbusNode:ModbusGroup:Variable:2").Get();
VariableJson variableJson = rootJson.GetSection("NodeClass:ModbusNode:ModbusGroup:Variable:1").Get();
Console.WriteLine("======XML============");
Console.WriteLine(variablexml.NameXML);
Console.WriteLine("=======Json===========");
Console.WriteLine(variableJson.Description);
Console.ReadKey();
//结果:
}
}
}
//结果:
实体类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace XMLFrameworkDemon
{
public class Variable
{
public string NameXML { get; set; }
public string Description { get; set; }
public string Type { get; set; }
public string VarAddress { get; set; }
public string Scale { get; set; }
public string Offset { get; set; }
public string Start { get; set; }
public string AccessProperty { get; set; }
public string AlarmEnable { get; set; }
public string ArchiveEnable { get; set; }
public string SetLimitEnable { get; set; }
public string AlarmType { get; set; }
public string DiscreteAlarmType { get; set; }
public string DiscreteAlarmNote { get; set; }
public string LoLoAlarmEnable { get; set; }
public string LoLoAlarmValue { get; set; }
public string LoLoAlarmPriority { get; set; }
public string LoLoAlarmNote { get; set; }
public string LowAlarmEnable { get; set; }
public string LowAlarmValue { get; set; }
public string LowAlarmPriority { get; set; }
public string LowAlarmNote { get; set; }
public string HighAlarmEnable { get; set; }
public string HighAlarmValue { get; set; }
public string HighAlarmPriority { get; set; }
public string HighAlarmNote { get; set; }
public string HiHiAlarmEnable { get; set; }
public string HiHiAlarmValue { get; set; }
public string HiHiAlarmPriority { get; set; }
public string HiHiAlarmNote { get; set; }
public string ArchivePeriod { get; set; }
public string SetLimitMax { get; set; }
public string SetLimitMin { get; set; }
public string VarType { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace XMLFrameworkDemon
{
public class VariableJson
{
public long Id { get; set; }
public string Number { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string Type { get; set; }
public string VarAddress { get; set; }
public string Scale { get; set; }
public string Offset { get; set; }
public string Start { get; set; }
public string AccessProperty { get; set; }
public string AlarmEnable { get; set; }
public string ArchiveEnable { get; set; }
public string SetLimitEnable { get; set; }
public string AlarmType { get; set; }
public string DiscreteAlarmType { get; set; }
public string DiscreteAlarmPriority { get; set; }
public string DiscreteAlarmNote { get; set; }
public string LoLoAlarmEnable { get; set; }
public string LoLoAlarmValue { get; set; }
public string LoLoAlarmPriority { get; set; }
public string LoLoAlarmNote { get; set; }
public string LowAlarmEnable { get; set; }
public string LowAlarmValue { get; set; }
public string LowAlarmPriority { get; set; }
public string LowAlarmNote { get; set; }
public string HighAlarmEnable { get; set; }
public string HighAlarmValue { get; set; }
public string HighAlarmPriority { get; set; }
public string HighAlarmNote { get; set; }
public string HiHiAlarmEnable { get; set; }
public string HiHiAlarmValue { get; set; }
public string HiHiAlarmPriority { get; set; }
public string HiHiAlarmNote { get; set; }
public string ArchivePeriod { get; set; }
public string SetLimitMax { get; set; }
public string SetLimitMin { get; set; }
public string VarType { get; set; }
public string StoreType { get; set; }
public string InsertTime { get; set; }
public string Value { get; set; }
public long ModbusGroupId { get; set; }
}
}
XML:
Json:
{
"ConnectionStrings": {
"SqliteConnectionString": "Data Source=E:\Csharp\EF6Demon\EF6Demon\bin\Debug\net6.0-windows\Database\DbSqlite.db",
"MySQLConnectionString": "server=192.168.85.102; database=OneToMany; uid=root; pwd=123456;"
},
"NodeClass": {
"Id": 1,
"Name": "ModbusClent",
"Description": "Modbus相关客户端",
"ModbusNode": {
"Id": 1,
"NodeClassId": 1,
"Name": "ModbusTCPClient",
"Description": "1#ZG上位机测试",
"ModbusType": "2000",
"ConnectTimeOut": "2000",
"CreateTime": "0",
"ReConnectTime": "5000",
"IsActive": "True",
"MaxErrorTimes": "1",
"KeyWay": "VarName",
"UseAlarmCheck": "True",
"ServerURL": "127.0.0.1",
"Port": "502",
"DataFormat": "ABCD",
"VarNum": "6",
"ModbusGroup": {
"Id": 1,
"Name": "保持寄存器长度为寄存器个数",
"Description": "40001-40010",
"Type": "ModbusTCP",
"StoreArea": "40000",
"Length": "10",
"Start": "0",
"SlaveID": "1",
"IsActive": "true",
"ModbusNodeId": 1,
"Variable": [
{
"Id": 1,
"Number": "1",
"Name": "Float1",
"Description": "40001-40002",
"Type": "ModbusTCP",
"VarAddress": 0,
"Scale": "1",
"Offset": "0",
"Start": "0",
"AccessProperty": "读写",
"AlarmEnable": "True",
"ArchiveEnable": "True",
"SetLimitEnable": "True",
"AlarmType": "True",
"DiscreteAlarmType": "False",
"DiscreteAlarmPriority": "0",
"DiscreteAlarmNote": "null",
"LoLoAlarmEnable": "True",
"LoLoAlarmValue": "0",
"LoLoAlarmPriority": "0",
"LoLoAlarmNote": "40001-40002低低报警",
"LowAlarmEnable": "True",
"LowAlarmValue": "20",
"LowAlarmPriority": "0",
"LowAlarmNote": "40001-40002低报警",
"HighAlarmEnable": "True",
"HighAlarmValue": "80",
"HighAlarmPriority": "0",
"HighAlarmNote": "40001-40002高报警",
"HiHiAlarmEnable": "True",
"HiHiAlarmValue": "100",
"HiHiAlarmPriority": "0",
"HiHiAlarmNote": "40001-40002高高报警",
"ArchivePeriod": "80",
"SetLimitMax": "100",
"SetLimitMin": "0",
"VarType": "Float",
"StoreType": "03 Holding Register(4x)",
"InsertTime": "0",
"Value": "0",
"ModbusGroupId": 1
},
{
"Id": 2,
"Number": "2",
"Name": "Float2",
"Description": "40003-40004",
"Type": "ModbusTCP",
"VarAddress": 2,
"Scale": "1",
"Offset": "0",
"Start": "0",
"AccessProperty": "读写",
"AlarmEnable": "True",
"ArchiveEnable": "True",
"SetLimitEnable": "True",
"AlarmType": "True",
"DiscreteAlarmType": "False",
"DiscreteAlarmPriority": "0",
"DiscreteAlarmNote": "null",
"LoLoAlarmEnable": "True",
"LoLoAlarmValue": "0",
"LoLoAlarmPriority": "0",
"LoLoAlarmNote": "40003-40004低低报警",
"LowAlarmEnable": "True",
"LowAlarmValue": "20",
"LowAlarmPriority": "0",
"LowAlarmNote": "40003-40004低报警",
"HighAlarmEnable": "True",
"HighAlarmValue": "80",
"HighAlarmPriority": "0",
"HighAlarmNote": "40003-40004高报警",
"HiHiAlarmEnable": "True",
"HiHiAlarmValue": "100",
"HiHiAlarmPriority": "0",
"HiHiAlarmNote": "40003-40004高高报警",
"ArchivePeriod": "80",
"SetLimitMax": "100",
"SetLimitMin": "0",
"VarType": "Float",
"StoreType": "03 Holding Register(4x)",
"InsertTime": "0",
"Value": "0",
"ModbusGroupId": 1
},
{
"Id": 3,
"Number": "3",
"Name": "Float3",
"Description": "40005-40006",
"Type": "ModbusTCP",
"VarAddress": 4,
"Scale": "1",
"Offset": "0",
"Start": "0",
"AccessProperty": "读写",
"AlarmEnable": "True",
"ArchiveEnable": "True",
"SetLimitEnable": "True",
"AlarmType": "True",
"DiscreteAlarmType": "False",
"DiscreteAlarmPriority": "0",
"DiscreteAlarmNote": "null",
"LoLoAlarmEnable": "True",
"LoLoAlarmValue": "0",
"LoLoAlarmPriority": "0",
"LoLoAlarmNote": "40005-40006低低报警",
"LowAlarmEnable": "True",
"LowAlarmValue": "20",
"LowAlarmPriority": "0",
"LowAlarmNote": "40005-40006低报警",
"HighAlarmEnable": "True",
"HighAlarmValue": "80",
"HighAlarmPriority": "0",
"HighAlarmNote": "40005-40006高报警",
"HiHiAlarmEnable": "True",
"HiHiAlarmValue": "100",
"HiHiAlarmPriority": "0",
"HiHiAlarmNote": "40005-40006高高报警",
"ArchivePeriod": "80",
"SetLimitMax": "100",
"SetLimitMin": "0",
"VarType": "Float",
"StoreType": "03 Holding Register(4x)",
"InsertTime": "0",
"Value": "0",
"ModbusGroupId": 1
},
{
"Id": 4,
"Number": "4",
"Name": "Float4",
"Description": "40007-40008",
"Type": "ModbusTCP",
"VarAddress": 6,
"Scale": "1",
"Offset": "0",
"Start": "0",
"AccessProperty": "读写",
"AlarmEnable": "True",
"ArchiveEnable": "True",
"SetLimitEnable": "True",
"AlarmType": "True",
"DiscreteAlarmType": "False",
"DiscreteAlarmPriority": "0",
"DiscreteAlarmNote": "null",
"LoLoAlarmEnable": "True",
"LoLoAlarmValue": "0",
"LoLoAlarmPriority": "0",
"LoLoAlarmNote": "40003-40004低低报警",
"LowAlarmEnable": "True",
"LowAlarmValue": "20",
"LowAlarmPriority": "0",
"LowAlarmNote": "40003-40004低报警",
"HighAlarmEnable": "True",
"HighAlarmValue": "80",
"HighAlarmPriority": "0",
"HighAlarmNote": "40003-40004高报警",
"HiHiAlarmEnable": "True",
"HiHiAlarmValue": "100",
"HiHiAlarmPriority": "0",
"HiHiAlarmNote": "40003-40004高高报警",
"ArchivePeriod": "80",
"SetLimitMax": "100",
"SetLimitMin": "0",
"VarType": "Float",
"StoreType": "03 Holding Register(4x)",
"InsertTime": "0",
"Value": "0",
"ModbusGroupId": 1
},
{
"Id": 5,
"Number": "5",
"Name": "Ushort1",
"Description": "40009",
"Type": "ModbusTCP",
"VarAddress": 8,
"Scale": "1",
"Offset": "0",
"Start": "0",
"AccessProperty": "读写",
"AlarmEnable": "True",
"ArchiveEnable": "True",
"SetLimitEnable": "True",
"AlarmType": "True",
"DiscreteAlarmType": "False",
"DiscreteAlarmPriority": "0",
"DiscreteAlarmNote": "null",
"LoLoAlarmEnable": "True",
"LoLoAlarmValue": "0",
"LoLoAlarmPriority": "0",
"LoLoAlarmNote": "40009低低报警",
"LowAlarmEnable": "True",
"LowAlarmValue": "20",
"LowAlarmPriority": "0",
"LowAlarmNote": "40009低报警",
"HighAlarmEnable": "True",
"HighAlarmValue": "80",
"HighAlarmPriority": "0",
"HighAlarmNote": "40009高报警",
"HiHiAlarmEnable": "True",
"HiHiAlarmValue": "100",
"HiHiAlarmPriority": "0",
"HiHiAlarmNote": "40009高高报警",
"ArchivePeriod": "80",
"SetLimitMax": "100",
"SetLimitMin": "0",
"VarType": "UShort",
"StoreType": "03 Holding Register(4x)",
"InsertTime": "0",
"Value": "0",
"ModbusGroupId": 1
},
{
"Id": 6,
"Number": "6",
"Name": "Ushort2",
"Description": "40010",
"Type": "ModbusTCP",
"VarAddress": 9,
"Scale": "1",
"Offset": "0",
"Start": "0",
"AccessProperty": "读写",
"AlarmEnable": "True",
"ArchiveEnable": "True",
"SetLimitEnable": "True",
"AlarmType": "True",
"DiscreteAlarmType": "False",
"DiscreteAlarmPriority": "0",
"DiscreteAlarmNote": "null",
"LoLoAlarmEnable": "True",
"LoLoAlarmValue": "0",
"LoLoAlarmPriority": "0",
"LoLoAlarmNote": "40009低低报警",
"LowAlarmEnable": "True",
"LowAlarmValue": "20",
"LowAlarmPriority": "0",
"LowAlarmNote": "40009低报警",
"HighAlarmEnable": "True",
"HighAlarmValue": "80",
"HighAlarmPriority": "0",
"HighAlarmNote": "40009高报警",
"HiHiAlarmEnable": "True",
"HiHiAlarmValue": "100",
"HiHiAlarmPriority": "0",
"HiHiAlarmNote": "40009高高报警",
"ArchivePeriod": "80",
"SetLimitMax": "100",
"SetLimitMin": "0",
"VarType": "UShort",
"StoreType": "03 Holding Register(4x)",
"InsertTime": "0",
"Value": "0",
"ModbusGroupId": 1
}
]
}
}
}
}



