How to return a subset of XElements inside parent XElement



If I have xml such as the following:



<ValidationResults>
<ValidationResult>
<Scheme>Scheme A</Scheme>
</ValidationResult>
<ValidationResult>
<Scheme>Scheme B</Scheme>
</ValidationResult>


I want to query it using Linq to Xml so that I return:



<ValidationResults>
<ValidationResult>
<Scheme>Scheme A</Scheme>
</ValidationResult>
</ValidationResults>


If I do something like this:



....Element("ValidationResults").Elements("ValidaitonResult")
.Where(x => x.Element("Scheme").Value == "Scheme A");


I return only:



<ValidationResult>
<Scheme>Scheme A</Scheme>
</ValidationResult>


Then I am currently creating the parent again and adding the children to it, which does not seem correct:



var parentElement = new XElement("ValidationResults");
var childElements = //get as above

parentElement.Add(childElements);


How do I return a queried subset of elements inside it's original parent?


No comments:

Post a Comment