C# - 使用 Linq 从 XML 文件加载哈希集字典
C# - Load dictionary of Hashsets from an XML file using Linq
我有一个 XML 文件,其中包含我想添加到哈希集字典中以供以后解析的标识符。
我对如何使用 linq 从 XML 文件中填充这个哈希集字典感到困惑。我曾尝试使用 Whosebug 上的其他帖子,但我的 XML 文件的填充方式与我看到的其他文件不同。
目前我的 XML 文件如下所示:
<Release_Note_Identifiers>
<Identifier container ="Category1">
<Container_Value>Old</Container_Value>
<Container_Value>New</Container_Value>
</Identifier>
<Identifier container ="Category2">
<Container_Value>General</Container_Value>
<Container_Value>Liquid</Container_Value>
</Identifier>
<Identifier container ="Category3">
<Container_Value>Flow Data</Container_Value>
<Container_Value>Batch Data</Container_Value>
</Identifier>
<Identifier container ="Category4">
<Container_Value>New Feature</Container_Value>
<Container_Value>Enhancement</Container_Value>
</Identifier>
</Release_Note_Identifiers>
我想将所有这些添加到 Dictionary<string, HashSet<string>>()
,其中键是每个类别,哈希集包含每个容器值。
我想尽可能抽象,因为我想最终添加更多类别并为每个类别添加更多容器值。
谢谢!
使用此设置代码:
var contents = @" <Release_Note_Identifiers>
<Identifier container =""Category1"">
<Container_Value>Old</Container_Value>
<Container_Value>New</Container_Value>
</Identifier>
<Identifier container =""Category2"">
<Container_Value>General</Container_Value>
<Container_Value>Liquid</Container_Value>
</Identifier>
<Identifier container =""Category3"">
<Container_Value>Flow Data</Container_Value>
<Container_Value>Batch Data</Container_Value>
</Identifier>
<Identifier container =""Category4"">
<Container_Value>New Feature</Container_Value>
<Container_Value>Enhancement</Container_Value>
</Identifier>
</Release_Note_Identifiers>";
var xml = XElement.Parse(contents);
...下面给你想要的
var dict = xml.Elements("Identifier")
.ToDictionary(
e => e.Attribute("container").Value,
e => new HashSet<string>(
e.Elements("Container_Value").Select(v=> v.Value)));
我有一个 XML 文件,其中包含我想添加到哈希集字典中以供以后解析的标识符。
我对如何使用 linq 从 XML 文件中填充这个哈希集字典感到困惑。我曾尝试使用 Whosebug 上的其他帖子,但我的 XML 文件的填充方式与我看到的其他文件不同。
目前我的 XML 文件如下所示:
<Release_Note_Identifiers>
<Identifier container ="Category1">
<Container_Value>Old</Container_Value>
<Container_Value>New</Container_Value>
</Identifier>
<Identifier container ="Category2">
<Container_Value>General</Container_Value>
<Container_Value>Liquid</Container_Value>
</Identifier>
<Identifier container ="Category3">
<Container_Value>Flow Data</Container_Value>
<Container_Value>Batch Data</Container_Value>
</Identifier>
<Identifier container ="Category4">
<Container_Value>New Feature</Container_Value>
<Container_Value>Enhancement</Container_Value>
</Identifier>
</Release_Note_Identifiers>
我想将所有这些添加到 Dictionary<string, HashSet<string>>()
,其中键是每个类别,哈希集包含每个容器值。
我想尽可能抽象,因为我想最终添加更多类别并为每个类别添加更多容器值。
谢谢!
使用此设置代码:
var contents = @" <Release_Note_Identifiers>
<Identifier container =""Category1"">
<Container_Value>Old</Container_Value>
<Container_Value>New</Container_Value>
</Identifier>
<Identifier container =""Category2"">
<Container_Value>General</Container_Value>
<Container_Value>Liquid</Container_Value>
</Identifier>
<Identifier container =""Category3"">
<Container_Value>Flow Data</Container_Value>
<Container_Value>Batch Data</Container_Value>
</Identifier>
<Identifier container =""Category4"">
<Container_Value>New Feature</Container_Value>
<Container_Value>Enhancement</Container_Value>
</Identifier>
</Release_Note_Identifiers>";
var xml = XElement.Parse(contents);
...下面给你想要的
var dict = xml.Elements("Identifier")
.ToDictionary(
e => e.Attribute("container").Value,
e => new HashSet<string>(
e.Elements("Container_Value").Select(v=> v.Value)));