Bla*_*tor 2 c# xml linq-to-xml
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>
Run Code Online (Sandbox Code Playgroud)
码:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
我有一个xml编写器程序,编辑上面的xml.Files元素可以更改为Folder元素.我有另一个程序读取值(文件位置)并用它做一些事情,我必须首先检查元素是文件或文件夹元素.
kon*_*nqi 11
你可以用类似的东西来检查元素是否存在
if(configuration.Elements("...").Any()){...}
Run Code Online (Sandbox Code Playgroud)
但我不确定你在这里问的是什么......
归档时间: |
|
查看次数: |
12016 次 |
最近记录: |