C# C-使用Linq从XML文件加载哈希集字典

C# C-使用Linq从XML文件加载哈希集字典,c#,xml,linq,dictionary,linq-to-xml,C#,Xml,Linq,Dictionary,Linq To Xml,我有一个包含标识符的XML文件,我想将其添加到哈希集字典中,以便稍后进行解析 我对如何使用linq从XML文件填充这个哈希集字典感到困惑。我曾尝试使用stackoverflow上的其他帖子,但我的XML文件的填充方式与我见过的其他帖子不同 当前我的XML文件如下所示: <Release_Note_Identifiers> <Identifier container ="Category1"> <Container_Value>

我有一个包含标识符的XML文件,我想将其添加到哈希集字典中,以便稍后进行解析

我对如何使用linq从XML文件填充这个哈希集字典感到困惑。我曾尝试使用stackoverflow上的其他帖子,但我的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>
我想将所有这些添加到字典中,其中键是每个类别,哈希集包含每个容器值

我希望使其尽可能抽象,因为我希望最终添加更多类别,并为每个类别添加更多容器值

谢谢大家!

使用此设置代码:

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文档的方式好吗?我注意到它与我在电视上看到的其他电影不同stackoverflow@user3369494当前位置如果不了解您正在处理的数据类型,很难说什么是好的。它看起来足够有效。