XML:
<CONFIGURATION>
<Files>
<File>D:\Test\TestFolder\TestFolder1\TestFile.txt</File>
<File>D:\Test\TestFolder\TestFolder1\TestFile01.txt</File>
<File>D:\Test\TestFolder\TestFolder1\TestFile02.txt</File>
<File>D:\Test\TestFolder\TestFolder1\TestFile03.txt</File>
<File>D:\Test\TestFolder\TestFolder1\TestFile04.txt</File>
</Files>
<SizeMB>3</SizeMB>
<BackupLocation>D:\Log backups\File backups</BackupLocation>
</CONFIGURATION>
码:
private void btnLinq_Click(object sender, EventArgs e)
{
queryData(@"D:\WatchMe1\backupconfig1.xml");
}
static void queryData(string xmlFile)
{
var xdoc = XDocument.Load(xmlFile);
var configuration = xdoc.Element("CONFIGURATION");
string sizeMB = configuration.Element("SizeMB").Value;
string backupLocation = configuration.Element("BackupLocation").Value;
//need a code here to check if element <File> exist before executing the file array
string[] files = configuration.Element("Files").Elements("File").Select(c => c.Value).ToArray();
foreach (string file in files)
{
Console.WriteLine(file);
}
}
我有一个编辑上述XML XML编写程序。 files元素可以改变到文件夹的元素。 我还有一个程序,读取的值(文件位置),并用它做什么,我先来检查,如果该元素是一个文件或文件夹的元素。