看到的一道题,用递归获取XML元素....
static void Main(string[] args) { string xmlContent = @""; var parentElement = XElement.Parse(xmlContent); var elements = FindElements(parentElement.Elements(), "File"); } public static IList FindElements(IEnumerable elements, string elementName) { if(elements==null||elements.Count()==0 ||string.IsNullOrWhiteSpace(elementName)) { return new List (); } var selectedElements = new List (); foreach (var element in elements) { if (element.HasElements) { selectedElements.AddRange(FindElements(element.Elements(), elementName)); } else { if (string.Equals(element.Name.LocalName, elementName)) { selectedElements.Add(element); } } } return selectedElements; }