none
Export database Data To XML File RRS feed

Answers

  • Hi,

    use a simple SELECT query and call either ExecuteReader or ExecuteScalar on your SqlCommand object.

    public string ReadXmlData(int ID)
    {
       string query = "SELECT XmlContent FROM dbo.xmlTB WHERE ID = @ID";
       string connectionString = "Data Source=SERVER1\\SQLEXPRESS;Initial Catalog=xml;Integrated Security=True;Pooling=False";
    
       using(SqlConnection conn = new SqlConnection(connectionString))
       using(SqlCommand cmd = new SqlCommand(query, conn))
       {
          cmd.Parameters.Add("@ID", SqlDbType.Int);
          cmd.Parameters["@ID"].Value = ID;
    
          conn.Open();
          string xmlContents = cmd.ExecuteScalar().ToString();
          conn.Close();
    
          return xmlContents;
       }
       catch (Exception)
       {
           return flag;
       }
    }


    One good question is equivalent to ten best answers.

    Wednesday, January 30, 2013 11:19 AM
  • Easy way can be having the SQL result in Dataset and you can use the DataSet.WriteXml property

    Mark Answered, if it solves your question and Vote if you found it helpful.
    Rohit Arora

    Friday, February 1, 2013 3:58 AM

All replies

  • Hi,

    use a simple SELECT query and call either ExecuteReader or ExecuteScalar on your SqlCommand object.

    public string ReadXmlData(int ID)
    {
       string query = "SELECT XmlContent FROM dbo.xmlTB WHERE ID = @ID";
       string connectionString = "Data Source=SERVER1\\SQLEXPRESS;Initial Catalog=xml;Integrated Security=True;Pooling=False";
    
       using(SqlConnection conn = new SqlConnection(connectionString))
       using(SqlCommand cmd = new SqlCommand(query, conn))
       {
          cmd.Parameters.Add("@ID", SqlDbType.Int);
          cmd.Parameters["@ID"].Value = ID;
    
          conn.Open();
          string xmlContents = cmd.ExecuteScalar().ToString();
          conn.Close();
    
          return xmlContents;
       }
       catch (Exception)
       {
           return flag;
       }
    }


    One good question is equivalent to ten best answers.

    Wednesday, January 30, 2013 11:19 AM
  • Easy way can be having the SQL result in Dataset and you can use the DataSet.WriteXml property

    Mark Answered, if it solves your question and Vote if you found it helpful.
    Rohit Arora

    Friday, February 1, 2013 3:58 AM
  • Use Rohit's Method if you want to dump all the data, or if you want to have some more logic in place and want to dump data manually go with Shyam's method.

    Thanks, Durgesh Chaudhary. http://home.techphernalia.com

    Wednesday, February 6, 2013 12:01 PM