title: How to check name of element with WriteEndElement
body: <p>I'm writing xml with <a href="http://msdn.microsoft.com/en-gb/library/system.xml.xmlwriter.aspx" rel="nofollow">XmlWriter</a>. My code has lots of sections like this:</p>
  
  <pre><code>xml.WriteStartElement("payload");
  ThirdPartyLibrary.Serialise(results, xml);
  xml.WriteEndElement(); // &lt;/payload&gt;
  </code></pre>
  
  <p>The problem is that the <code>ThirdPartyLibrary.Serialise</code> method is unreliable. It can happen (depending on the variable <code>results</code>) that it doesn't close all the tags it opens. As a consequence, my WriteEndElement line is perverted, consumed closing the library's hanging tags, rather than writing <code>&lt;/payload&gt;</code>.</p>
  
  <p>Thus I'd like to make a checked call to WriteEndElement that checks the element name, and throws an exception unless the cursor is at the expected element. </p>
  
  <pre><code>xml.WriteEndElement("payload");
  </code></pre>
  
  <p>You can think of this like <code>XmlReader.ReadStartElement(name)</code> which throws unless the cursor is at the expected place in the document.</p>
  
  <p>How can I achieve this?</p>
  
  <hr>
  
  <p>Edit: A second use case for this extension method would be to make my own code more readable and reliable.</p>