c# - Iterating through a list of paths from a config file? -
i have console app few foreach loops iterate through paths , parses out email addresses csv files. however, need loop read config file instead of going through directory, call existing api endpoint each email address. have now:
static void runtask(string[] args) { foreach (string folders in directory.enumeratedirectories("c:\\tests"))//looks @ every folder within main folder called tests --this needs read list of paths { foreach (string path in directory.enumeratefiles(folders, "*.csv"))//looks @ every file extension ".csv" in each folder { debug.write("\n" + path + "\n"); //writes out file names using (streamreader sr = new streamreader(path)) { string line; while ((line = sr.readline()) != null) { string[] parts = line.split(','); string email = parts[1]; debug.write(email + "\n");//writes out email column } } } } }
this what's in path.config file:
<pathssettings> <paths> <add path="c:\tests\first" templateid="123456"> </add> <add path="c:\tests\second" templateid="tem_56hynijcxxgp52zrgdwzic "> </add> <add path="c:\tests\third" templateid="tem_2wwt6yfgkdxsntepkhhcwb "> </add> </paths> </pathssettings>
i'm not sure how should writing foreach draw list of these paths.
public class pathtemplate { public string path { get; set; } public string template { get; set; } } public static string getxml() { return "<pathssettings>" + "<paths>" + "<add path=\"c:\\tests\\first\" templateid=\"123456\"></add>" + "<add path=\"c:\\tests\\second\" templateid=\"tem_56hynijcxxgp52zrgdwzic \"></add>" + "<add path=\"c:\\tests\\third\" templateid=\"tem_2wwt6yfgkdxsntepkhhcwb \"></add>" + "</paths>" + "</pathssettings>"; } var xdoc = xdocument.parse(getxml()); console.writeline(xdoc); var pts = xdoc.elements("pathssettings") .elements("paths") .elements("add"). select(p => new pathtemplate() { path = p.attribute("path").value.tostring(), template = p.attribute("templateid").value.tostring() }).tolist();
Comments
Post a Comment