我有一个带有“ OrderInfo”堆的“ OrderList”。每个“ OrderInfo”只有一个“ Documentcode”,最多四个“ Documentid”。如何找到哪个DocumentId属于哪个Documentcode?我有以下xml:
<OrderList>
<OrderInfo>
<DocList>
<DocumentInfo>
<Documentid>12</Documentid>
</DocumentInfo>
<DocumentInfo>
<Documentid>22</Documentid>
</DocumentInfo>
</DocList>
<Documentcode>ABC2</Documentcode>
</OrderInfo>
<OrderInfo>
<DocList>
<DocumentInfo>
<Documentid>11</Documentid>
</DocumentInfo>
<DocumentInfo>
<Documentid>25</Documentid>
</DocumentInfo>
</DocList>
<Documentcode>ABC3</Documentcode>
</OrderInfo>
和:
var documentId = myXml.SelectNodes("/OrderList/OrderInfo/DocList/DocumentInfo/Documentid");
我得到了Documentid的总数。但是,如何在“ OrderInfo”内部循环并找出“ Documentcode”-“ DocumentId”对?例如:
ABC2=12
ABC2=22
ABC3=11
ABC3=25
如果有这个我可以创建字典。
我写了Linq的这一小段文字,它可能无法很好地抵御缺少的水平/元素。
var doc = XElement.Load(fileName);
Dictionary<string, string> dic = doc
.Descendants("Documentid")
.ToDictionary(e => e.Value,
e => e.Parent.Parent.Parent.Element("Documentcode").Value );
// verify
Console.WriteLine(dic["12"]);
Console.WriteLine(dic["25"]);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句