title: C# Change XML value using Xpath
body: <p>can anyone help me? I am kind of new to C# but getting there. 
  I have been set a task to retrieve data from an XML file, change the data, and save it in another location using Xpath.</p>
  
  <p>I think I have the code 'kind of' correct but I am struggling with the syntax with the path to the specific element. I have included a sample of the XML file which has been simplified but the layout of the elements is the same.</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;Data SomeData which linked to a wepage.&gt;
      &lt;DeviceId&gt;Sometext&lt;/DeviceId&gt;
      &lt;Inputs&gt;
          &lt;a:ImageInput&gt;
              &lt;a:Cal&gt;
                  &lt;a:Volt1&gt;&lt;/Volt1&gt;
                  &lt;a:Volt2&gt;&lt;/Volt2&gt;
              &lt;/a:Cal&gt;
              &lt;a:Name&gt;Name1&lt;/a:Name&gt;
          &lt;/a:ImageInput&gt;
          &lt;a:ImageInput&gt;
              &lt;a:Cal&gt;
                  &lt;a:Volt1&gt;&lt;/Volt1&gt;
                  &lt;a:Volt2&gt;&lt;/Volt2&gt;
              &lt;/a:Cal&gt;
              &lt;a:Name&gt;Name2&lt;/a:Name&gt;  &lt;!-- Need to change this element --&gt;
          &lt;/a:ImageInput&gt;
      &lt;/Inputs&gt;
  &lt;/Data&gt;
  </code></pre>
  
  <p>As you can see I need to change the a:Name element but within the second a:ImageInput element. I have looked for answers but I have only found examples where Xpath has changed part of a root element.</p>
  
  <p>Here is my code which is used within the Main() method.  </p>
  
  <pre><code>public static void ChangeFileXPath()//Changes the value of the node using Xpath
      {
          XmlDocument xmld = new XmlDocument();
          xmld.Load(@"C:\ProgramData\Oxford Instruments NanoAnalysis\Calibration\mics_simulator.xml");
  
          XmlElement name2 = (XmlElement)xmld.SelectSingleNode("/MicsModule/Inputs/a:ImageInput[@a:Name='BSE']");
          if (name2 != null)
          {
              name2.SetAttribute("a:Name", "{{16}}");
          }
  
          string AmmendedFile = @"C:\ProgramData\Oxford Instruments NanoAnalysis\XXXX NewXMLReader\Xpath_Mics_Sim.xml";
          xmld.Save(AmmendedFile);
      }
  </code></pre>
  
  <p>Any help would be very appreciated. Thank you for looking. </p>