📰XML文件
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<userSettings>
<Internal.Settings type="Desktop">
<setting name="StatsDisplayCount" serializeAs="String">
<value>10</value>
</setting>
<setting name="EnableUndo" serializeAs="String">
<value>False</value>
</setting>
</Internal.Settings>
<Internal.Settings type="Desktop1">
<setting name="DBConnection" serializeAs="String">
<value>数据库身份验证</value>
</setting>
<setting name="AdvanceOption" serializeAs="String">
<value />
</setting>
</Internal.Settings>
</userSettings>
</configuration>
💻C#代码
static void Main(string[] args)
{
string currentPath = System.AppDomain.CurrentDomain.BaseDirectory;
DirectoryInfo directory = new DirectoryInfo(string.Format(@"{0}..\..\", currentPath));
string UserConfigPath = Path.Combine(directory.FullName, "test.xml");
if (File.Exists(UserConfigPath))
{
XmlDocument xml = new XmlDocument();
xml.Load(UserConfigPath);
XmlNode node = xml.SelectSingleNode("/configuration/userSettings/Internal.Settings/setting[@name='StatsDisplayCount']");
if (node.InnerText != "")
{
Console.WriteLine(node.InnerText);
}
else
{
Console.WriteLine("空");
}
}
Console.ReadKey();
}
📺 结果