Quantcast
Channel: Using LINQ to aggregate multiple nested elements in XDocument - Stack Overflow
Viewing all articles
Browse latest Browse all 2

Using LINQ to aggregate multiple nested elements in XDocument

$
0
0

I have the following XML (parsed into an XDocument):

XDocument nvdXML = XDocument.Parse(@"<entry id='CVE-2016-1926'><vulnerable-configuration id='http://www.nist.gov/'><logical-test operator='OR' negate='false'><fact-ref name='A'/><fact-ref name='B'/><fact-ref name='C'/></logical-test></vulnerable-configuration><vulnerable-configuration id='http://www.nist.gov/'><logical-test operator='OR' negate='false'><fact-ref name='X'/><fact-ref name='Y'/><fact-ref name='Z'/></logical-test></vulnerable-configuration></entry>");

I want to get a single collection/list of every "name" attribute for each entry (in this case there is only one entry, whose name list would consist of ['A','B','C','X','Y','Z'])

Here is the code I have:

var entries = from entryNodes in nvdXML.Descendants("entry")                          select new CVE                          {                              //VulnerableConfigurations = (from vulnCfgs in entryNodes.Descendants(vulnNS +"vulnerable-configuration").Descendants(cpeNS +"logical-test")                              //                            select new VulnerableConfiguration                              //                            {                              //                                Name = vulnCfgs.Element(cpeNS +"fact-ref").Attribute("name").Value                              //                            }).ToList()                              VulnerableConfigurations = (from vulnCfgs in entryNodes.Descendants("vulnerable-configuration")                                                          from logicalTest in vulnCfgs.Descendants("logical-test")                                                          select new VulnerableConfiguration                                                          {                                                              Name = logicalTest.Element("fact-ref").Attribute("name").Value                                                          }).ToList()                          };

Unfortunately, this (both commented and uncommented) query only results in VulnerableConfigurations ['A','X'], instead of ['A','B','C','X','Y','Z']

How do I modify my query such that every element of every list is selected (assuming there could be 1+ nested lists)?

Note, I did search for dup's, and although there are similar questions, most are very specific, and ask for grouping/summing/manipulation, or are not related to XML parsing.


Final working code (thanks to accepted answer):

var entries = from entryNodes in nvdXML.Descendants("entry")                          select new CVE                          {                              VulnerableConfigurations = (from vulnCfgs in entryNodes.Descendants("fact-ref")                                                          select new VulnerableConfiguration                                                          {                                                              Name = vulnCfgs.Attribute("name").Value                                                          }).ToList()                          };

Viewing all articles
Browse latest Browse all 2

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>
<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596344.js" async> </script>