// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; using System.Collections.Generic; using System.Linq; using System.Xml.Linq; namespace E { public class A { public Int32 Id { get; set; } public IList Bs { get; set; } } public class B { public Int32 Id { get; set; } } public class Program { public static void Main(string[] args) { string axml = @" 1 2 "; string bxml = @" 1 2 3 "; string abxml = @" 1 1 2 1 2 3 "; XDocument adoc = XDocument.Parse(axml); XDocument bdoc = XDocument.Parse(bxml); XDocument abdoc = XDocument.Parse(abxml); List alst = new List(); foreach(int aid in from a in adoc.Root.Elements("A") select int.Parse(a.Element("Id").Value)) { alst.Add(new A { Id=aid, Bs=(from b in abdoc.Root.Elements("AB") where int.Parse(b.Element("AId").Value)==aid select new B { Id=int.Parse(b.Element("BId").Value) }).ToList() }); } foreach(A aelm in alst) { Console.WriteLine(aelm.Id); foreach(B belm in aelm.Bs) { Console.Write(" " + belm.Id); } Console.WriteLine(); } } } }