title: How to read/update <runtime> element in machine.config?
body: <p>As per <a href="https://stackoverflow.com/questions/18542812/powershell-cmdlet-missing-assembly-google-api">this issue</a> I am attempting to make a method that will check the current state of the machine.config file (to see if the runtime alterations exist) and if not, to update or remove them. My <a href="http://pastebin.com/ZqMvh5Nr" rel="nofollow noreferrer">current solution</a> is using XmlDocument to write, but my attempts at checking for existing elements on the next run always returns null or false:</p>
  
  <pre><code>XmlDocument doc = new XmlDocument();
  doc.Load(file);
  Console.WriteLine(null == doc.SelectSingleNode("/configuration"))); //true
  Console.WriteLine(null == 
      doc.SelectSingleNode("/configuration/runtime"))); //true
  Console.WriteLine(null == 
      doc.SelectSingleNode("/configuration/runtime/assemblyBinding"))); //false
  </code></pre>
  
  <p>My second attempt was to use Linq to XML to look for the data, but not knowing how that really works I couldn't get any results:</p>
  
  <pre><code>XDocument doc = XDocument.Load(RuntimeEnvironment.SystemConfigurationFile);
  var data = from item in root.Descendants("assemblyIdentity") select el;
  
  var foo = doc.Descendants("assemblyIdentity")
      .Attributes("publicKeyToken")
      .Where(x =&gt; x.Value == @"b03f5f7f11d50a3a")
      .ToList();
  
  Console.WriteLine(foo.Count); //shows 0
  </code></pre>
  
  <p>My third attempt was to use the <a href="http://msdn.microsoft.com/en-us/library/System.Configuration.ConfigurationManager%28v=vs.110%29.aspx" rel="nofollow noreferrer">ConfigurationManager class</a> to read the file, but whereas I was able to open the file and read it I could only get my hands on the runtime element, but not any of the contained information. At best I have been able to check the raw xml data:</p>
  
  <pre><code>Configuration machineConfig =
      ConfigurationManager.OpenMachineConfiguration();
  
  ConfigurationFileMap configFile =
      new ConfigurationFileMap(machineConfig.FilePath);
  
  Configuration config =
      ConfigurationManager.OpenMappedMachineConfiguration(
          configFile);
  
  ConfigurationSectionCollection sections =
      config.Sections;
  
  ConfigurationSection runtime = config.GetSection("runtime");
  
  Console.WriteLine(runtime.SectionInformation.GetRawXml()
      .Contains("System.Runtime")); //true
  </code></pre>
  
  <p>Since I didn't see any built-in support for the runtime element (like it does for <a href="http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.appsettings%28v=vs.110%29.aspx" rel="nofollow noreferrer">AppSettings</a>) I tried to create a <a href="http://msdn.microsoft.com/en-us/library/2tw134k3.aspx" rel="nofollow noreferrer">custom configuration section</a> (<a href="http://pastebin.com/PHSRF6um" rel="nofollow noreferrer">view that code here</a>) but have been unable to do anything with it:</p>
  
  <pre><code>RuntimeSection runtimeSection = (RuntimeSection)config.GetSection("runtime");
  Console.WriteLine(runtimeSection.SectionInformation.GetRawXml()); //nothing
  </code></pre>
  
  <p>SO, that being said, how can I check the machine.config file to make sure the  element changes are not already in place?</p>