I think the best would be to use XmlElement.ElementsWithName as the starting point. So I have the following enhanced method that returns AllElementswithName, internally it runs recursively :
XmlDocumentHelper = public extension class(XmlDocument)
public
method AllElementsWithName(aLocalName: not nullable String; aNamespace: nullable XmlNamespace := nil;
aXmlElement: XmlElement := nil): not nullable sequence of XmlElement;
begin
var lXmlElement: XmlElement;
if not assigned(aXmlElement) then
lXmlElement := Root
else
lXmlElement := aXmlElement;
if lXmlElement.Elements.Count = 0 then
exit(new List<XmlElement>);
result := lXmlElement.ElementsWithName(aLocalName, aNamespace);
for each el in lXmlElement.Elements do
result := (not nullable sequence of XmlElement)(result.Concat(AllElementsWithName(aLocalName, aNamespace, el)));
end;
end;
And to retrieve all elements of an xml document with a specified name, we now just call:
var xmlDoc: XmlDocument := XmlDocument.FromFile('MyHolyShit.xml');
// All elements in the xml doc with name "item" are returned.
var items := xmlDoc.AllElementsWithName('item');
This works but I wonder if there is any other more elegant way to accomplish this?