title: How can I access a single XML element's value using C#.net web-pages with WebMatrix?
body: <p>I've looked at a lot of resources, done a lot of research, and tried many "best-guesses" to access a single element at a time using WebMatrix with C#, web-pages, however nothing I am trying is getting through.</p>
  
  <p>Consider a simple xml document that looks like this:</p>
  
  <pre><code>&lt;root&gt;
      &lt;requisitionData&gt;
          &lt;element1&gt;I am element 1&lt;/element1&gt;
          &lt;element2&gt;I am element 2&lt;/element2&gt;
      &lt;/requisitionData&gt;
  &lt;/root&gt;
  </code></pre>
  
  <p>I know I can use a foreach loop, like so:</p>
  
  <pre><code>@using System.Xml.Linq
  
  XDocument doc = XDocument.Load(Server.MapPath("~/User_Saves/cradebaugh/testFile.xml"));
  
  foreach (XElement element in doc.Descendants("requisitionData"))
  {
      @element.Value
  }
  </code></pre>
  
  <p>And that, of course, works fine.  But what if I simply wanted to store the single element, <code>&lt;element1&gt;</code>'s value in a string variable?</p>
  
  <p>I've looked here (link below), but I can't make heads or tails of this code (it barely even looks like C# to me, but then again, I'm so new to parsing XML...):</p>
  
  <p><a href="http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/b14ce4d1-77f1-420d-ad91-0989794a1d45/" rel="nofollow noreferrer">http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/b14ce4d1-77f1-420d-ad91-0989794a1d45/</a></p>
  
  <p>I've also checked here: <a href="https://stackoverflow.com/questions/752271/how-to-get-xml-node-from-xdocument">How to Get XML Node from XDocument</a></p>
  
  <p>But the code shown makes no sense to me here either.  I keep thinking there must be a simpler way to do this, hopefully without learning a whole new querying approach.</p>
  
  <p>---------------------------------THINGS I'VE TRIED---------------------------------</p>
  
  <pre><code>XDocument doc = XDocument.Load(Server.MapPath("~/User_Saves/cradebaugh/testFile.xml"));
  
  string element = doc.Descendants("requisitionData").Descendants("element1").Value;
  </code></pre>
  
  <p><strong>Error I receive:</strong> "missing using directive or assembly reference</p>
  
  <pre><code>XDocument doc = XDocument.Load(Server.MapPath("~/User_Saves/cradebaugh/testFile.xml"));
  
  XElement element = doc.Descendants("element1");
  string val = element.Value;
  </code></pre>
  
  <p><strong>Error I receive:</strong>  Cannot implicitly convert type 'System.Collections.Generic.IEnumerable' to 'System.Xml.Linq.XElement'. An explicit conversion exists (are you missing a cast?)</p>
  
  <p>I have, indeed, tried other things, but I get pretty much the same errors as shown above. Am I making this harder than it is, or am I oversimplifying it?</p>
  
  <p>-------------------------UPDATE------------------------------</p>
  
  <p>I was able to get this to work:</p>
  
  <pre><code>string element = doc.Element("root").Element("requisitionData").Element("element1").Value;
  
  @element
  </code></pre>
  
  <p>However, one thing that concerns me about this approach is that <code>.Element</code> selects the 'first' match, so in an xml document that looks like this:</p>
  
  <pre><code>&lt;root&gt;
      &lt;requisitionData&gt;
          &lt;element1&gt;I am element 1&lt;/element1&gt;
          &lt;element2&gt;I am element 2&lt;/element2&gt;
      &lt;/requisitionData&gt;
      &lt;requisitionData&gt;
          &lt;element1&gt;I am element 1&lt;/element1&gt;
          &lt;element2&gt;I am element 2&lt;/element2&gt;
      &lt;/requisitionData&gt;
  &lt;/root&gt;
  </code></pre>
  
  <p>How could I access the second occurrence of <code>&lt;element1&gt;</code>?</p>