title: XmlWriter stops writing to StringBuilder
body: <p>I have a strange problem. When looping through an array and creating XML with XMLWriter and a StringBuilder the strings just stop getting added to the string builder. I don't get any errors until the XmlDoc.LoadXml method is called when I get this error:</p>
  
  <p>Unexpected end of file has occurred. The following elements are not closed: ID, Port, Ports. Line 1256, position 39.</p>
  
  <p>When I stepped though the code the loops continue and throw no errors but nothing is added to the string builder.</p>
  
  <p>Any Ideas? Thanks</p>
  
  <pre><code>public XmlElement LclExportGetPorts()
      {
          DAL.DALSoapClient soapy = new DAL.DALSoapClient();
          DAL.DALStringString[] ports = soapy.EnumPortsWeb(false);
          XmlDocument XmlDoc = new XmlDocument();
          StringBuilder SB = new StringBuilder();
          XmlWriterSettings XmlSettings = new XmlWriterSettings();
          XmlSettings.Indent = true;
          using (XmlWriter writer = XmlWriter.Create(SB, XmlSettings))
          {
              writer.WriteStartDocument();
              writer.WriteStartElement("Ports");
              foreach (var p in ports)
              {
                  writer.WriteStartElement("Port");
                  writer.WriteElementString("ID", p.Key);
                  writer.WriteElementString("Name", p.Value);
                  writer.WriteEndElement();
              }
              writer.WriteEndElement();
              writer.WriteEndDocument();
              string temp = writer.ToString();
              XmlDoc.LoadXml(SB.ToString());
          }
          XmlElement Result = XmlDoc.DocumentElement;
          return Result;
      }
  </code></pre>