【问题标题】:c# - how to add an xml field with multiple values to a dictionaryc# - 如何将具有多个值的xml字段添加到字典中
【发布时间】:2020-12-15 00:59:06
【问题描述】:

我编写了一个 C# 代码,它将一个 xml 字段及其值转换为一个字典,但我没有注意到我收到的 XML 文件有一些包含多个值的字段(ConditionAncestorTerm),就像在下一个块中一样:

<StudyFieldsResponse>
<APIVrs>1.01.02</APIVrs>
  <DataVrs>2020:12:06 23:30:13.949</DataVrs>
  <Expression>ca045-011</Expression>
  <NStudiesAvail>359831</NStudiesAvail>
  <NStudiesFound>1</NStudiesFound>
  <MinRank>1</MinRank>
  <MaxRank>20</MaxRank>
  <NStudiesReturned>1</NStudiesReturned>
  <FieldList>
    <Field>CompletionDate</Field>
    <Field>Condition</Field>
    <Field>ConditionAncestorTerm</Field>
  </FieldList>
  <StudyFieldsList>
    <StudyFields Rank="1">
      <FieldValues Field="CompletionDate">
        <FieldValue>January 17, 2026</FieldValue>
      </FieldValues>
      <FieldValues Field="Condition">
        <FieldValue>Renal Cell Carcinoma</FieldValue>
      </FieldValues>
      <FieldValues Field="ConditionAncestorTerm">
        <FieldValue>Neoplasms, Glandular and Epithelial</FieldValue>
        <FieldValue>Neoplasms by Histologic Type</FieldValue>
        <FieldValue>Neoplasms</FieldValue>
        <FieldValue>Adenocarcinoma</FieldValue>
        <FieldValue>Kidney Neoplasms</FieldValue>
        <FieldValue>Urologic Neoplasms</FieldValue>
        <FieldValue>Urogenital Neoplasms</FieldValue>
        <FieldValue>Neoplasms by Site</FieldValue>
        <FieldValue>Kidney Diseases</FieldValue>
        <FieldValue>Urologic Diseases</FieldValue>
      </FieldValues>

我将 XML 元素转换为字典的 C# 代码起初看起来像这样:

static Dictionary<string, string> DecodeXML(XDocument study)
        {
            // Convert XML to a dictionary
            var data = study
                .Elements("StudyFieldsResponse")
                .Elements("StudyFieldsList")
                .Elements("StudyFields")
                .Elements("FieldValues")
                .ToDictionary(
                    key => key.Attribute("Field").Value,
                    value => value.Element("FieldValue").Value
                    );

            return data;
        }

我的代码现在中断了,因为有些字段有多个值,我猜这个程序不太好。 我希望能够将与一个字段对应的所有这些值添加到列表中,所以我想我应该将字典更改为 以使其工作,但我还没有找到一种方法来存储每一个值到列表中。 有谁知道我该如何解决这个问题?

【问题讨论】:

    标签: c# xml


    【解决方案1】:

    尝试以下:

               XDocument doc = XDocument.Load(FILENAME);
    
                Dictionary<string, XElement> dict = doc.Descendants("FieldValues")
                    .GroupBy(x => (string)x.Attribute("Field"), y => y)
                    .ToDictionary(x => x.Key, y => y.FirstOrDefault());
    

    或者这个

                XDocument doc = XDocument.Load(FILENAME);
    
                Dictionary<string, List<string>> dict = doc.Descendants("FieldValues")
                    .GroupBy(x => (string)x.Attribute("Field"), y => y)
                    .ToDictionary(x => x.Key, y => y.Elements("FieldValue").Select(z => (string)z).ToList());
    

    【讨论】:

    • 谢谢,第二个答案就可以了!我真的不明白第一个块。这只获取第一个值吗?因为它说 y.FirstorDefault()?
    • 第一个解决方案获取 FieldValues 和所有的孩子。
    【解决方案2】:

    先加载xml再创建字典

    `

    var xml= XDocument.Load("XMLFileName.xml");
    
        var data= xml.Document.Root.Elements("StudyFieldsResponse")
                            .Elements("StudyFieldsList")
                            .Elements("StudyFields")
                            .Elements("FieldValues")
                            .ToDictionary(
                                  x => x.Attribute("Field").Value,
                                  x => x.Elements("FieldValue")
                            .Select(y => y.Value)
                            .ToArray());
    

    `

    如果你分享你想要显示的输出会更有帮助。

    【讨论】:

      猜你喜欢
      • 2023-03-31
      • 1970-01-01
      • 1970-01-01
      • 2022-01-23
      • 2017-01-04
      • 1970-01-01
      • 1970-01-01
      • 2021-01-28
      • 2014-06-27
      相关资源
      最近更新 更多