最近,我建立了一个小型转换器,以某种结构将txt数据转换为xml,我选择了一个文件夹,程序遍历该文件夹中的所有文件,并以XML
格式一起写入一个xml文档。
在文件夹中,我的数据名称如下:
Data.0001.txt
Data.0002.txt
Data.0003.txt
Data.0004.txt
Data.txt
以此类推
我只希望其中不包含零的文件,因为具有零的文件只是其他文件的备份副本,而且我有6000多个文件,我无法手动过滤它们
到目前为止,这是我的代码
static void Main(string[] args)
{
FolderBrowserDialog SelectFolder = new FolderBrowserDialog();
String path = @"C:\newpages";
XmlDocument doc = new XmlDocument();
XmlElement root = doc.CreateElement("Pages");
if (SelectFolder.ShowDialog() == DialogResult.OK)
{
var txt = string.Empty;
string[] Files = Directory.GetFiles((SelectFolder.SelectedPath));
int i = 1;
foreach (string path1 in Files)
{
String filename = Path.GetFileNameWithoutExtension((path1));
using (StreamReader sr = new StreamReader(path1))
{
txt = sr.ReadToEnd();
XmlElement id = doc.CreateElement("Page.id");
id.SetAttribute("Page.Nr", i.ToString());
id.SetAttribute("Pagetitle", filename);
XmlElement name = doc.CreateElement("PageContent");
XmlCDataSection cdata = doc.CreateCDataSection(txt);
name.AppendChild(cdata);
id.AppendChild(name); // page id appenndchild
root.AppendChild(id); // roots appenedchild
doc.AppendChild(root); //Main root
}
i++;
}
}
Console.WriteLine("finished");
Console.ReadKey();
doc.Save(Path.ChangeExtension(path, ".xml"));
}
}
任何帮助都是非常好的人
答案 0 :(得分:2)
GetFiles
返回指定目录中的文件名。其返回类型为string[]
,因此您可以轻松地应用Where
来过滤file
的名称,如下所示:-
var files = Directory.GetFiles("PathToYourDirec").Where(name => !name.Contains("0"));
答案 1 :(得分:1)
在字符串文件名上,您可以确保它不包含“ 0”
if(!filename.Contains("0"))
{
}
答案 2 :(得分:0)
在Files
变量上,您可以使用正则表达式过滤掉仅包含字母的文件名
var reg = new Regex(@"^([^0-9]*)$");
var files = Directory.GetFiles("path-to-folder")
.Where(path => reg.IsMatch(path))
.ToList();
答案 3 :(得分:0)
解决此问题时,可以大大简化整个代码。您不需要StreamReader即可读取整个文件,还可以及早获取文件名并进行过滤,而不用进入foreach和过滤:
static void Main(string[] args)
{
FolderBrowserDialog SelectFolder = new FolderBrowserDialog();
String path = @"C:\newpages";
XmlDocument doc = new XmlDocument();
XmlElement root = doc.CreateElement("Pages");
if (SelectFolder.ShowDialog() == DialogResult.OK)
{
// Don't declare txt here, you're overwriting and only using it in a nested loop, declare it as you use it there
// var txt = string.Empty;
//string[] Files = Directory.GetFiles((SelectFolder.SelectedPath));
// Change to getting FileInfos
var Files = new DirectoryInfo(SelectFolder.SelectedPath).GetFiles()
// Only keep those who don't contain a zero in file name
.Where(f=>!f.Name.Contains("0"));
int i = 1;
foreach (var file in Files)
{
//String filename = Path.GetFileNameWithoutExtension((path1));
// Don't need a StreamReader not a using block, just read the whole file at once with File.ReadAllText
//using (StreamReader sr = new StreamReader(path1))
//{
//txt = sr.ReadToEnd();
var txt = File.ReadAllText(file.FullName);
XmlElement id = doc.CreateElement("Page.id");
id.SetAttribute("Page.Nr", i.ToString());
id.SetAttribute("Pagetitle", file.FullName);
XmlElement name = doc.CreateElement("PageContent");
XmlCDataSection cdata = doc.CreateCDataSection(txt);
name.AppendChild(cdata);
id.AppendChild(name); // page id appenndchild
root.AppendChild(id); // roots appenedchild
doc.AppendChild(root); //Main root
//}
i++;
}
}
Console.WriteLine("finished");
Console.ReadKey();
doc.Save(Path.ChangeExtension(path, ".xml"));
}
我还建议您不要使用您正在使用的XML api,而要使用更新和更简单的linq to XML,因为这也将简化元素的创建,请参见下面的整个代码的非常简化版本: d用LINQ和XElements编写过
static void Main(string[] args)
{
FolderBrowserDialog SelectFolder = new FolderBrowserDialog();
String path = @"C:\newpages";
var root = new XElement("Pages");
if (SelectFolder.ShowDialog() == DialogResult.OK)
{
var FilesXML = new DirectoryInfo(SelectFolder.SelectedPath).GetFiles()
.Where(f => !f.Name.Contains("0"))
// Note that the index is 0 based, if you want to start with 1 just replace index by index+1 in Page.Nr
.Select((file, index) =>
new XElement("Page.id",
new XAttribute("Page.Nr",index),
new XAttribute("Pagetitle",file.FullName),
new XElement("PageContent",
new XCData(File.ReadAllText(file.FullName))
)));
// Here we already have all your XML ready, just need to add it to the root
root.Add(FilesXML);
}
Console.WriteLine("finished");
Console.ReadKey();
root.Save(Path.ChangeExtension(path, ".xml"));
}
答案 4 :(得分:-1)
您可以尝试这样做,但是我建议您是否更改创建备份文件名称的逻辑。它不应以“ 0”作为字符,而应在文件名中提及诸如“ backup”之类的固定文本。
static void Main(string[] args)
{
FolderBrowserDialog SelectFolder = new FolderBrowserDialog();
String path = @"C:\newpages";
XmlDocument doc = new XmlDocument();
XmlElement root = doc.CreateElement("Pages");
if (SelectFolder.ShowDialog() == DialogResult.OK)
{
var txt = string.Empty;
string[] Files = Directory.GetFiles((SelectFolder.SelectedPath));
int i = 1;
foreach (string path1 in Files)
{
String filename = Path.GetFileNameWithoutExtension((path1));
if (!filename.Contains(".0"))
{
using (StreamReader sr = new StreamReader(path1))
{
txt = sr.ReadToEnd();
XmlElement id = doc.CreateElement("Page.id");
id.SetAttribute("Page.Nr", i.ToString());
id.SetAttribute("Pagetitle", filename);
XmlElement name = doc.CreateElement("PageContent");
XmlCDataSection cdata = doc.CreateCDataSection(txt);
name.AppendChild(cdata);
id.AppendChild(name); // page id appenndchild
root.AppendChild(id); // roots appenedchild
doc.AppendChild(root); //Main root
}
}
i++;
}
}
Console.WriteLine("finished");
Console.ReadKey();
doc.Save(Path.ChangeExtension(path, ".xml"));
}