C#解析簡單的XML文件,我有這個很簡單的XML文件:與LINQ
<?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());
}
}
}
+1:非常有用的,從來沒有使用ToDictionary – evanb