title: Execute Stored Procedure and get and save xml c#
body: <p>I have been trying to execute a stored procedure, returning xml, and saving that xml to a file with asp.net c#.  I have tried many things and have used bits and pieces from other posts here on stack overflow.  My procedure works fine when I run it in sql server management studio; however, I cannot seem to get it to right correctly.  The saved file always comes back blank or with one line.  Is there anything I am doing wrong that somebody can see?  I am very amature at doing this and would apreciate any help I can get.  See code below...</p>
  
  <pre><code>SqlConnection conn = new SqlConnection(@"Server=server\ERP;DataBase=dbase;User ID=id;Password=pw");
      conn.Open();
  
      SqlCommand cmd = new SqlCommand("Labels_printer", conn);
      cmd.CommandType = CommandType.StoredProcedure;
  
      cmd.Parameters.Add(new SqlParameter("@job_control", jobControl));
  
      cmd.Parameters.Add(new SqlParameter("@from", from));
  
      cmd.Parameters.Add(new SqlParameter("@to", to));
  
      cmd.Parameters.Add(new SqlParameter("@factory", 88));
  
      cmd.Parameters.Add(new SqlParameter("@printer", printer));
      cmd.CommandTimeout = 1000;
      //System.Xml.XmlReader myXmlReader = cmd.ExecuteXmlReader();
      using (XmlReader reader = cmd.ExecuteXmlReader()) //e.command.commandtimeout = 500
      {
  
          while (reader.Read())
          {
            //  string s = reader.ReadOuterXml();
  
  
              XmlDocument dom = new XmlDocument();
              dom.Load(reader);
  
              var settings = new XmlWriterSettings();
              settings.Indent = true;
              settings.OmitXmlDeclaration = true;
  
                  using (var writer = XmlTextWriter.Create(@"\\file\.xml", settings))
                  {
                      dom.WriteContentTo(writer);
  
  
  
              }
          }
          reader.Close();
      }
  </code></pre>