我有一个控制台应用程序,其中包含一些foreach循环,这些循环遍历路径并从csv文件中解析出电子邮件地址。但是,我需要我的循环从配置文件中读取而不是通过目录,然后最终为每个电子邮件地址调用现有的API端点。这就是我现在所拥有的:
static void RunTask(string[] args)
{
foreach (string folders in Directory.EnumerateDirectories("C:\\tests"))//looks at every folder within this main folder called tests --this needs to read the list of paths
{
foreach (string path in Directory.EnumerateFiles(folders, "*.csv"))//looks at every file with the 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
}
}
}
}
}
这是我的Path.Config文件中的内容:
<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>
我不确定如何编写我的foreach来从这些路径列表中绘制。
答案 0 :(得分:0)
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();
答案 1 :(得分:0)
如果对象正确,可以反序列化xml。 我会说你的课程应该如下:
[Serializable()]
public class add
{
[System.Xml.Serialization.XmlAttribute("Path")]
public string Path { get; set; }
[System.Xml.Serialization.XmlAttribute("TemplateId")]
public string TemplateId { get; set; }
}
[Serializable()]
[System.Xml.Serialization.XmlRoot("pathsSettings")]
public class pathsSettings
{
[XmlArray("paths")]
[XmlArrayItem("add", typeof(add))]
public add[] paths { get; set; }
}
你可以像这样迭代它
public void DoWhatever(){
pathsSettings myObject;
// Construct an instance of the XmlSerializer with the type
// of object that is being deserialized.
XmlSerializer mySerializer = new XmlSerializer(typeof(pathsSettings));
// To read the file, create a FileStream.
FileStream myFileStream = new FileStream(@"yourpath\Path.config", FileMode.Open);
// Call the Deserialize method and cast to the object type.
myObject = (pathsSettings)mySerializer.Deserialize(myFileStream);
foreach(add addObj in myObject.paths)
{
Console.WriteLine(addObj.Path);
Console.WriteLine(addObj.TemplateId);
}
}
此方法要求您拥有正确的对象,但从长远来看,如果需要,您可以使用这些对象序列化您的值,从而编辑您的配置文件。如果您还需要TemplateId,那么您已经在“添加”对象
中答案 2 :(得分:0)
这对我有用:
var doc = XDocument.Parse(@"<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 paths =
doc
.Root
.Descendants("add")
.Select(e => e.Attribute("Path").Value)
.ToList();
这给了我这些路径:
C:\tests\first
C:\tests\second
C:\tests\third