Recursive Linq
Hi i have the following problem:
I have a XML File and i want to build c# objects in a list.
The Problem is that some of the entries are outdated and have a "deprecated" and a "replacement" Attribute.
In my List of objects i want to get a reference on the replaced Object.
For Example:
------Start XML Content:
<pdsc url="https://www.silabs.com/documents/public/cmsis-packs/" vendor="SiliconLabs" name="BGM21_DFP" version="5.8.10" deprecated="2019-11-12" replacement="SiliconLabs.GeckoPlatform_BGM21_DFP" /> <pdsc url="https://www.silabs.com/documents/public/cmsis-packs/" vendor="SiliconLabs" name="GeckoPlatform_BGM13_DFP" version="4.3.0" /> <pdsc url="https://www.silabs.com/documents/public/cmsis-packs/" vendor="SiliconLabs" name="GeckoPlatform_BGM21_DFP" version="4.3.0" />
Stop XML Content-------
------ Start Linq Query:
XDocument doc = XDocument.Load(xmlFile); var query1 = from c in doc.Descendants("pdsc") select new PackageDescription { Name = c.Attribute("name").Value, Url = Uri.TryCreate(c.Attribute("url").Value, UriKind.Absolute, out _) ? new Uri(c.Attribute("url").Value) : null, Vendor = c.Attribute("vendor").Value, Version = c.Attribute("version").Value, Deprecated = c.Attribute("deprecated") != null ? DateOnly.Parse(c.Attribute("deprecated").Value) : null, }; var list = query1.ToList();
End Linq Query ------
Is there an elegant way to write the query?
Bests
Brian
Comments
Here could be the answer, with whole examples: