title: C# how to foreach loop an XML document and modify a value
body: <p>I have an XML document that I want to modify the connectionstrings to.  How do I do a foreach loop and in this example modify the value of LocalSqlServer?</p>
  
  <pre><code>&lt;connectionStrings&gt;
      &lt;clear /&gt;
      &lt;add name="Localip" connectionString="Data Source=db01;Initial Catalog=TestA;Integrated Security=True;"
       providerName="System.Data.SqlClient" /&gt;
      &lt;add name="LocalSqlServer" connectionString="Data Source=db02;Failover Partner=db01;Initial Catalog=TestB;Integrated Security=True;"
       providerName="System.Data.SqlClient" /&gt;
      &lt;add name="ServerAp" connectionString="Data Source=LAPTOP;Initial Catalog=testc;Integrated Security=True;" providerName="System.Data.SqlClient"/&gt;
  &lt;/connectionStrings&gt;
  </code></pre>
  
  <p>This is what I've tried, but I really just want to modify the value not the whole content.  For this example I want to change:</p>
  
  <pre><code>&lt;add name="LocalSqlServer" connectionString="Data Source=db02;Failover Partner=db01;Initial Catalog=TestB;Integrated Security=True;"
       providerName="System.Data.SqlClient" /&gt;
  
  &lt;add name="LocalSqlServer" connectionString="Data Source=db07;Failover Partner=db07;Initial Catalog=TestB;Integrated Security=True;"
       providerName="System.Data.SqlClient" /&gt;
  </code></pre>
  
  <p>This is what I've tried:</p>
  
  <pre><code>System.Xml.XmlDocument xmlDocument = new System.Xml.XmlDocument();
          xmlDocument.Load(@"C:\xml.xml");
          XmlNodeList elemList = xmlDocument.GetElementsByTagName("connectionStrings");
          for (int i = 0; i &lt; elemList.Count; i++)
          {
              foreach (XmlNode chldNode in elemList[i].ChildNodes)
              {
                  Console.WriteLine(chldNode.Name.ToString());
                  if (chldNode.Name.ToString() == "add")
                  {
                      foreach (XmlAttribute xmlAtt in chldNode.Attributes)
                      {
                          if (xmlAtt.Value == "LocalSqlServer")
                          {
                              xmlAtt.InnerXml = "MyNewValue";
                              xmlDocument.Save(@"C:\xml2.xml");
                              break;
                          }
                      }
                  }
              }
          }
  </code></pre>