title: How to update XML attributes using Linq to XML in C#
body: <p>I have a collection of custom objectswhich has storeid. I also have an XmlDocument which I query data from the database and constructed in memory. The StoreId property in the custome object corresponds to the "Value" object in the XML.I have to loop through my custom collection and match the StoreId to the "Value" attribute in the XML and set its "Checked" attribute equal to true. As shown in the XML below, all the "Checked" attribute values are set to false at the start.  </p>
  
  <pre><code>&lt;Tree&gt;
  &lt;Node Text="Whole "&gt;
    &lt;Node Text="America"&gt;
       &lt;Node Text="NewYork"&gt;
          &lt;Node Value="28" Checked="false " Text="NY1" /&gt;
          &lt;Node Value="29" Checked="false " Text="NY2" /&gt;
       &lt;/Node&gt;
       &lt;Node Text="Houston"&gt;
          &lt;Node Value="13 " Checked="false " Text="H1" /&gt;
          &lt;Node Value="14 " Checked="false " Text="H2" /&gt;
          &lt;Node Value="16 " Checked="false " Text="H3" /&gt;
          &lt;Node Value="19 " Checked="false " Text="H4" /&gt;
          &lt;Node Value="26 " Checked="false " Text="H5" /&gt;
       &lt;/Node&gt;
       &lt;Node Text="GeorgeTown"&gt;
          &lt;Node Value="21 " Checked="false " Text="G1" /&gt;
          &lt;Node Value="23 " Checked="false " Text="G2" /&gt;
          &lt;Node Value="25 " Checked="false " Text="G3" /&gt;
       &lt;/Node&gt;
    &lt;/Node&gt;
    &lt;/Node&gt;
    &lt;/Tree&gt;
  </code></pre>
  
  <p>My code is:</p>
  
  <pre><code>public class MyObject
   {
      public string StoreId { get; set; }
      public bool HasValue { get; set; }
   }
  
   Code to update XML:
   XmlDocument baseDocument = ConstructXMLFromDataBase();
   XmlNodeList dataNodes = baseDocument.SelectNodes("//Tree/Node/Node/Node");
  
   List&lt;MyObject&gt; myCollections = GetMyCollection();
  
   foreach (var myCollection in myCollections)
   {
      foreach (XmlNode node in dataNodes)
      {
        //code to update
      }               
   }
  </code></pre>
  
  <p>I believe it can be done easily with Linq to XML and I am very new to Linq to XML. Moreover most of the samples available on the internet are about updating XML loaded from a disc and not constructed in memory.</p>
  
  <p>Thanks</p>