Firstly, there is absolutely no sense that this is a ref parameter. It is possible that you do not understand the parameters of ref - see my article on this subject .
Since this is naturally recursive, I would probably write it like this:
public static List<XDocument> GetResources(string startURL) { List<XDocument> ret = new List<XDocument>(); GetResourcesRecursive(startURL, ret); return ret; } private static void GetResourcesRecursive(string startURL, List<XDocument> result) { var doc = XDocument.Parse(GetXml(startURL)); var xs = new XmlSerializer(typeof(resourceList)); var rdr = doc.CreateReader(); if (xs.CanDeserialize(rdr)) { var rl = (resourceList)xs.Deserialize(doc.CreateReader()); foreach (var item in rl.resourceURL) { GetResourcesRecursive(startURL + item.location, ref result); } } else { result.Add(doc); } }
You can save it in a recursive way and create a new list at each level, but it feels a little ugly for me. The above gives you the public API you want, but without highlighting the collections on the left, right, and center.
Now you can write it in a non-recursive way, basically by creating a queue of URLs to work through:
public static List<XDocument> GetResources(string startURL) { List<XDocument> ret = new List<XDocument>(); Queue<string> urls = new Queue<string>(); urls.Enqueue(startUrl); while (urls.Count > 0) { string url = urls.Dequeue(); var doc = XDocument.Parse(GetXml(url)); var xs = new XmlSerializer(typeof(resourceList)); var rdr = doc.CreateReader(); if (xs.CanDeserialize(rdr)) { var rl = (resourceList) xs.Deserialize(doc.CreateReader()); foreach (var item in rl.resourceURL) { queue.Enqueue(url + item.location); } } else { ret.Add(doc); } } return ret; }
Itβs too late a day for me to find out if this gives the results in the same order β I suspect this is not the case β but hopefully this is not important.
(You really don't have type resourceList , you? resourceList , please!)
source share