I have an ´OrderList´ with multipile ´OrderInfo´. Each ´OrderInfo´ has only one ´Documentcode´ and maximum four ´Documentid´. How can I find which DocumentId belongs to which Documentcode? I have the following 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>
with:
var documentId = myXml.SelectNodes("/OrderList/OrderInfo/DocList/DocumentInfo/Documentid");
I get the total number of Documentid's. But how can I loop inside ´OrderInfo´ and find out the pairs "Documentcode"-´DocumentId´? For example:
ABC2=12
ABC2=22
ABC3=11
ABC3=25
If I have this I can create Dictionaries.
I wrote this little piece of Linq , it may not be very resilient against missing levels/elements.
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"]);