首页 > 解决方案 > 验证配置文件 C# 的内容

问题描述

我需要验证现有配置文件的内容。

它看起来像这样:

<configuration>
   <appSettings>
      <Version>HB.2017.0</Version>
      <FORMAT_VERSION>2.4</FORMAT_VERSION>
      <MISC>Stuff.2014.0</MISC>
   </appSettings>
</configuration>

我一直在尝试用 C# 编写一些东西来读取文件并分配 and 的内容,Version然后Format-Version验证它是否为真,但我不断收到空指针错误。

这是我到目前为止所拥有的:

public void ValidateConfigVersionSetting()
    {

         XmlDocument doc = new XmlDocument();
         doc.Load(@"C:\project.exe.config");

         XmlNode node = doc.DocumentElement.SelectSingleNode("/Version");

         string nodeContent = node.InnerText;

         if (nodeContent.Equals("2017.0"))
             {
                Report.Success("Config", "Config is correct! 2017.");
             }
         else
            {
                Report.Failure("Config", "Config is not 2017.");
            }    

    }

这是 Ranorex 自动化套件的代码模块,因此Validate.IsTrue来自它。我存储的方式是否innertext正确?

标签: c#xmlranorex

解决方案


如果该代码与编写的完全相同,那么您在这里有一个错误:

Validate.IsTrue(nodeContent="HB.2017.0", "Config is proper");

您将字符串分配给 nodeContent,而不是比较它。


推荐阅读