c#-如何将具有多个值的xml字段添加到字典中
我编写了一个C#代码,将xml字段及其值转换为字典,但我没有注意到的是,我收到的xml文件中有一些字段包含多个值(ConditionAncestorTerm),如下一块所示:c#-如何将具有多个值的xml字段添加到字典中,c#,xml,C#,Xml,我编写了一个C#代码,将xml字段及其值转换为字典,但我没有注意到的是,我收到的xml文件中有一些字段包含多个值(ConditionAncestorTerm),如下一块所示: <StudyFieldsResponse> <APIVrs>1.01.02</APIVrs> <DataVrs>2020:12:06 23:30:13.949</DataVrs> <Expression>ca045-011</Expres
<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>
1.01.02
2020:12:06 23:30:13.949
ca045-011
359831
1.
1.
20
1.
完工日期
条件
条件存储期限
2026年1月17日
肾细胞癌
腺性和上皮性肿瘤
组织学类型
肿瘤
腺癌
肾肿瘤
泌尿系统肿瘤
泌尿生殖系统肿瘤
按部位划分的肿瘤
肾脏疾病
泌尿系疾病
我将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;
}
静态字典解码XML(XDocument研究)
{
//将XML转换为字典
var数据=研究
.要素(“研究领域响应”)
.要素(“研究领域列表”)
.要素(“研究领域”)
.元素(“字段值”)
.ToDictionary(
key=>key.Attribute(“Field”).Value,
value=>value.Element(“FieldValue”).value
);
返回数据;
}
我的代码现在中断了,因为一些字段有多个值,我猜程序不会接受这些值。
我希望能够将与一个字段对应的所有值添加到列表中,因此我认为应该将字典更改为a以使其正常工作,但我还没有找到将每个值存储到列表中的方法。
有人知道我该怎么做吗?试试下面的方法:
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(文件名);
Dictionary dict=文档子体(“字段值”)
.GroupBy(x=>(字符串)x.Attribute(“字段”),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());
XDocument doc=XDocument.Load(文件名);
Dictionary dict=文档子体(“字段值”)
.GroupBy(x=>(字符串)x.Attribute(“字段”),y=>y)
.ToDictionary(x=>x.Key,y=>y.Elements(“FieldValue”)。选择(z=>(string)z.ToList());
首先加载xml,然后创建字典
`
`
如果您共享要显示的输出,则会更有帮助。谢谢,第二个答案就可以了!我不太明白第一个街区。这只是第一个值吗?因为它说的是y.FirstorDefault(),所以第一个解决方案得到字段值和所有子项。
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());