C# 从XML文件创建节点对
我有一个带有多文件“OrderInfo”的“OrderList”。每个“OrderInfo”只有一个“Documentcode”,最多有四个“Documentid”。如何找到哪个DocumentId属于哪个Documentcode?我有以下xml:C# 从XML文件创建节点对,c#,xml,selectnodes,C#,Xml,Selectnodes,我有一个带有多文件“OrderInfo”的“OrderList”。每个“OrderInfo”只有一个“Documentcode”,最多有四个“Documentid”。如何找到哪个DocumentId属于哪个Documentcode?我有以下xml: <OrderList> <OrderInfo> <DocList> <DocumentInfo> <Document
<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>
我得到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"]);
var doc=XElement.Load(文件名);
字典dic=doc
.后代(“文档ID”)
.ToDictionary(e=>e.Value,
e=>e.Parent.Parent.Parent.Element(“Documentcode”).Value);
//核实
控制台写入线(dic[“12”);
控制台写入线(dic[“25”);
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"]);