我有这个非常简单的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());
}
}
}
答案 0 :(得分:11)
将其加载为XDocument
,因为它是文档,而不是元素。
var xDoc = XDocument.Load(configurationPAthFileTextbox.Text);
您可以轻松地将文档转换为Dictionary<string, string>
,其中元素名称为键,元素值为值:
var results = xDoc.Root
.Elements()
.ToDictionary(e => e.Name, e => (string)e);
打印ConfigurationFilePath
和ConnectionString
:
Console.WriteLine("{0}, {1}", results["ConfigurationFilePath"], results["ConnectionString"]);
打印Test1, Test2
。
答案 1 :(得分:1)
首先,创建一个类来表示XML文件中包含的感兴趣的信息。定义一个构造函数,从XML文件中提取感兴趣的值,并将它们映射到类的属性:
public class ConfigurationFile
{
public String ConfigurationFilePath { get; set; }
public String ConnectionString { get; set; }
public String AnalyzeFilePath { get; set; }
public ConfigurationFile(String xmlFilePath)
{
XDocument document = XDocument.Load(xmlFilePath);
var root = document.Root;
ConfigurationFilePath = (string)root.Element("ConfigurationFilePath");
ConnectionString = (string)root.Element("ConnectionString");
AnalyzeFilePath = (string)root.Element("AnalyzeFilePath");
}
}
使用特殊构造函数创建此类后,使用该类非常简单:
var configFile = new ConfigurationFile(xmlFilePath);
var path = configFile.ConfigurationFilePath;
var connectString = configFile.ConnectionString;
var analyzeFilePath = configFile.AnalyzeFilePath;
这是一个将所有内容组合在一起的演示程序。 (请注意,在此演示程序中,构造函数从字符串而不是文件中加载XML,如上所示。)
using System;
using System.Xml;
using System.Xml.Linq;
class LinqToXmlDemo
{
static public void Main(string[] args)
{
string xmlContent = GetXml();
var configFile = new ConfigurationFile(xmlContent);
Console.WriteLine
("ConfigurationFilePath:[{0}]\n" +
"ConnectionString:[{1}]\n" +
"AnalyzeFilePath:[{2}]\n--",
configFile.ConfigurationFilePath,
configFile.ConnectionString,
configFile.AnalyzeFilePath);
}
static string GetXml()
{
return
@"<?xml version='1.0' encoding='UTF-8'?>
<ConfigurationFile>
<ConfigurationFilePath>Test1</ConfigurationFilePath>
<ConnectionString>Test2</ConnectionString>
<AnalyzeFilePath>Test3</AnalyzeFilePath>
</ConfigurationFile>";
}
}
public class ConfigurationFile
{
public String ConfigurationFilePath { get; set; }
public String ConnectionString { get; set; }
public String AnalyzeFilePath { get; set; }
public ConfigurationFile(String xml)
{
XDocument document = XDocument.Parse(xml);
var root = document.Root;
ConfigurationFilePath = (string)root.Element("ConfigurationFilePath");
ConnectionString = (string)root.Element("ConnectionString");
AnalyzeFilePath = (string)root.Element("AnalyzeFilePath");
}
}
预期输出
ConfigurationFilePath:[Test1]
ConnectionString:[Test2]
AnalyzeFilePath:[Test3]
--
答案 2 :(得分:0)
简单地说,您也可以使用:
string yourString = (string)(from element in xDocument.Root.Descendants("ConfigurationFilePath") select element).First();
= d