验证配置文件C#

时间:2018-06-04 20:10:46

标签: c# xml ranorex

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

看起来像这样:

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

我一直在尝试用C#写一些内容来读取文件并分配VersionFormat-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的方式是否正确?

1 个答案:

答案 0 :(得分:4)

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

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

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

相关问题