title: C# retrieve multiple XML attributes from specific elements for manipulation
body: <p>Beginner C# person here and I have been wrestling with a particular scenario that i'd like your help with. I am in need of processing an XML file whose content can be different each time I read it. Each time I read that document, I need to search for specific attributes within specific elements. The composition of these specific elements and attributes may vary from file to file. I have been successful in reading a single element and a single attribute within that element using examples I've found here on this forum which leverage Linq to XML. Please see below for an example of the XML I am working with. </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8" standalone="yes"?&gt;
  &lt;w:document xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" mc:Ignorable="w14 w15 wp14"&gt;
      &lt;w:body&gt;
          &lt;w:p w14:paraId="2CBBB1B4" w14:textId="77777777" w:rsidR="00D9548A" w:rsidRDefault="00D9548A" w:rsidP="00ED7A0B"&gt;&lt;/w:p&gt;
          &lt;w:p w14:paraId="2CBBB1B5" w14:textId="77777777" w:rsidR="00ED548A" w:rsidRPr="00ED77B9" w:rsidRDefault="00C706DD" w:rsidP="00D9548A"&gt;&lt;/w:p&gt;
          &lt;w:pPr&gt;
              &lt;w:rPr&gt;
                  &lt;w:rFonts w:ascii="Arial" w:hAnsi="Arial" w:cs="Arial"&gt;&lt;/w:rFonts&gt;
                  &lt;w:b&gt;&lt;/w:b&gt;
                  &lt;w:sz w:val="40"&gt;&lt;/w:sz&gt;
                  &lt;w:szCs w:val="40"&gt;&lt;/w:szCs&gt;
              &lt;/w:rPr&gt;
          &lt;/w:pPr&gt;
          &lt;w:r w:rsidRPr="00cool6F"&gt;&lt;/w:r&gt;
          &lt;w:tr w:rsidR="0029258E" w14:paraId="2CBBB242" w14:textId="77777777" w:rsidTr="0029258E"&gt;&lt;/w:tr&gt;
      &lt;/w:body&gt;
  &lt;/w:document&gt;
  </code></pre>
  
  <p>The interesting elements would be 'w:p', 'w:r', 'w:tr' or pretty much any element which has any of the following attributes : wrsidR, wrsidRDefault, wrsidRPr, rsidTr. </p>
  
  <p>Ideally, I'd like a way to read in each of these values into some sort of list \ array so that I can change the values and write those values right back to the attributes I grabbed them from.</p>
  
  <p>The code I pieced together only returns 1 attribute 'rsidR' from the the 'w:p' elements in the file.</p>
  
  <pre><code>public static void foobar()
      {
          string strFile = @"C:\SourceFolder\SampleXML\document-test.xml";
          XDocument xDoc = XDocument.Load(strFile);
          XNamespace xNsp = xDoc.Root.Name.Namespace;
  
          var values = from rsids in xDoc.Descendants(xNsp + "p").Attributes(xNsp + "rsidR")
                       select rsids.Value.ToString();
  
          foreach (var v in values)
          {
              Console.WriteLine(v);
          }
      }
  </code></pre>
  
  <p>What is the best way to go after all the interesting attribute values in this file so that I can iterate through them, change the value and write it back to the XML file? </p>
  
  <p>As always, I do appreciate your help!</p>