我有一个非常简单的xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<ConfigurationFile>
<ConfigurationFilePath>Test1</ConfigurationFilePath>
<ConnectionString>Test2</ConnectionString>
<AnalyzeFilePath>Test3</AnalyzeFilePath>
</ConfigurationFile>
我希望获取每个字段的信息。 但是它没有显示任何内容。
以下是我的C#代码:
private void ParseXMLFile()
{
Console.WriteLine("Parse");
if (configurationPAthFileTextbox.Text != null)
{
Console.WriteLine("file != null");
try
{
XElement main = XElement.Load(configurationPAthFileTextbox.Text);
var results = main.Descendants("ConfigurationFile")
.Select(e => new { ConfigurationFilePath = e.Descendants("ConfigurationFilePath").FirstOrDefault().Value,
ConnectionString = e.Descendants("ConnectionString").FirstOrDefault().Value });
foreach (var result in results)
Console.WriteLine("{0}, {1}", result.ConfigurationFilePath, result.ConnectionString);
Console.ReadLine();
}
catch (Exception e)
{
Debug.WriteLine(e.ToString());
}
}
}