添加项目文件。

This commit is contained in:
Administrator
2026-08-04 18:36:40 +08:00
parent 16fb9e4f5a
commit a2ecbc795d
616 changed files with 149853 additions and 0 deletions
+74
View File
@@ -0,0 +1,74 @@
using System;
using System.Collections.Generic;
using System.Xml.Linq;
namespace WindowsService1
{
public class XmlConfigReader
{
private Dictionary<string, Dictionary<string, Dictionary<string, string>>> deviceData;
public XmlConfigReader(string xmlContent)
{
XDocument doc = XDocument.Parse(xmlContent);
deviceData = ReadXmlToNestedDictionary(doc);
}
public XmlConfigReader(XDocument doc)
{
deviceData = ReadXmlToNestedDictionary(doc);
}
private Dictionary<string, Dictionary<string, Dictionary<string, string>>> ReadXmlToNestedDictionary(XDocument doc)
{
var result = new Dictionary<string, Dictionary<string, Dictionary<string, string>>>();
var mainDevice = doc.Root.Element("Device");
string mainDeviceName = mainDevice.Attribute("name").Value;
result[mainDeviceName] = new Dictionary<string, Dictionary<string, string>>();
foreach (var subDevice in mainDevice.Elements("Device"))
{
string subDeviceName = subDevice.Attribute("name").Value;
result[mainDeviceName][subDeviceName] = new Dictionary<string, string>();
foreach (var type in subDevice.Elements("Type"))
{
string typeName = type.Attribute("name").Value;
string typeValue = type.Value;
result[mainDeviceName][subDeviceName][typeName] = typeValue;
}
}
return result;
}
public void PrintStructure()
{
foreach (var mainDevice in deviceData)
{
Console.WriteLine($"Main Device: {mainDevice.Key}");
foreach (var subDevice in mainDevice.Value)
{
Console.WriteLine($" Sub Device: {subDevice.Key}");
foreach (var type in subDevice.Value)
{
Console.WriteLine($" Type: {type.Key}, Value: {type.Value}");
}
}
}
}
public bool TryGetValue(string mainDeviceName, string subDeviceName, string typeName, out string value)
{
value = null;
return deviceData.TryGetValue(mainDeviceName, out var mainDevice) &&
mainDevice.TryGetValue(subDeviceName, out var subDevice) &&
subDevice.TryGetValue(typeName, out value);
}
public Dictionary<string, Dictionary<string, Dictionary<string, string>>> GetAllData()
{
return deviceData;
}
}
}