【发布时间】: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;
}
我的代码现在中断了,因为有些字段有多个值,我猜这个程序不太好。
我希望能够将与一个字段对应的所有这些值添加到列表中,所以我想我应该将字典更改为
【问题讨论】: