title: how to search for a xml node value, then create new attribute for that element in c#
body: <p>I am using c# .net 4.6 xpath to search for node with id value and when found create a new attribute for the parent element in place. I have a list of such id values that I need to iterate over and create attributes to produce a new xml document. I have attempted the following but does not work. The XPathNavigator.MoveTo method appears to replace the source navigator with the moved to element thereby loosing all other content. Is this not the right way to achieve this ? Could you please advice ?</p>
  <p>See code snippet below:</p>
  <pre><code>publicationDoc.LoadXml(publicationXPathNav.OuterXml);
              XPathNavigator publicationNav = publicationDoc.CreateNavigator();
  
      foreach (IListBlobItem item in contentDirectory.ListBlobs())
                  {
                      var blob = (CloudBlob)item;
                      string contentId = blob.Name;
                      
                      XPathNavigator contentRefNav = publicationNav.SelectSingleNode($@&quot;//releaseItem/contentRef[id = {&quot;'&quot; + contentId + &quot;'&quot;}]/..&quot;);
      
                      if (contentRefNav != null)
                      {
                          publicationNav.MoveTo(contentRefNav); // here publicationNav gets replaced by contentRefNav
                          publicationNav.CreateAttribute(&quot;&quot;, &quot;fileName&quot;, &quot;&quot;, contentFileName);
                      }
                  }
  
  // once finished with the foreach I was hoping to be able to save the publicationNav.OuterXml to a new file with the newly added attributes.
  </code></pre>
  <p>Here is a small cut down sample source xml data :</p>
  <pre><code>&lt;publicationsRoot&gt;
      &lt;publication&gt;
          &lt;urn&gt;iso:pub:std:FDIS:74824&lt;/urn&gt;
          &lt;releaseItems&gt;
              &lt;releaseItem&gt;
                  &lt;languageNeutral&gt;false&lt;/languageNeutral&gt;
                  &lt;type&gt;STANDARD&lt;/type&gt;
                  &lt;contentRef&gt;
                      &lt;id&gt;92764155&lt;/id&gt;
                  &lt;/contentRef&gt;
              &lt;/releaseItem&gt;
              &lt;releaseItem&gt;
                  &lt;languageNeutral&gt;false&lt;/languageNeutral&gt;
                  &lt;type&gt;STANDARD&lt;/type&gt;
                  &lt;contentRef&gt;
                      &lt;id&gt;92802320&lt;/id&gt;
                  &lt;/contentRef&gt;
              &lt;/releaseItem&gt;
              &lt;releaseItem&gt;
                  &lt;languageNeutral&gt;false&lt;/languageNeutral&gt;
                  &lt;type&gt;STANDARD&lt;/type&gt;
                  &lt;contentRef&gt;
                      &lt;id&gt;92801989&lt;/id&gt;
                  &lt;/contentRef&gt;
              &lt;/releaseItem&gt;
          &lt;releaseItems&gt;  
      &lt;/publication&gt;
  &lt;/publicationsRoot&gt;
  </code></pre>