Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Generate xml file from sql server in asp.net

Generate xml file from sql server in asp.net

Scheduled Pinned Locked Moved ASP.NET
csharpasp-netdatabasesql-serversysadmin
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    NET India
    wrote on last edited by
    #1

    Hello Friends, I'm generating a xml file from sql table in asp.net(c#). Here is my code given below using (SqlConnection mCon = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString())) { mCon.Open(); string strsql = "Select * From MakeTable Where Active=1 For XML Auto,ELEMENTS"; //string strsql = "Select * From MakeTable Where Active=1 "; SqlCommand mDataCom = new SqlCommand(); mDataCom.Connection = mCon; mDataCom.CommandText = strsql; mDataCom.CommandTimeout = 15; DataSet ds = new DataSet(); XmlReader xmlReader = null; xmlReader = mDataCom.ExecuteXmlReader(); ds.ReadXml(xmlReader); ds.DataSetName = "Halo"; ds.WriteXml("F://Manoj//Autoneed/Rough//XMLFile2.xml"); } but it's giving me output as shown here <MakeTable> <MakeID>10</MakeID> <MakeNameID>36</MakeNameID> <MakeYear>2009</MakeYear> <Active>1</Active> </MakeTable> but if i run my sql command in sql then it's giving me following records as shown below <MakeTable> <MakeID>1</MakeID> <MakeNameID>17</MakeNameID> <MakeYear>2009</MakeYear> <Active>1</Active> </MakeTable> <MakeTable> <MakeID>3</MakeID> <MakeNameID>17</MakeNameID> <MakeYear>2006</MakeYear> <Active>1</Active> </MakeTable> <MakeTable> <MakeID>4</MakeID> <MakeNameID>17</MakeNameID> <MakeYear>2007</MakeYear> <Active>1</Active> </MakeTable> <MakeTable> <MakeID>5</MakeID> <MakeNameID>17</MakeNameID> <MakeYear>2005</MakeYear> <Active>1</Active> </MakeTable> <MakeTable> <MakeID>7</MakeID> <MakeNameID>17</MakeNameID> <MakeYear>2004</MakeYear> <Active>1</Active> </MakeTable> can anybody let me know how can i get this output or where and what change i should make in my code or sqlcommand

    C W 2 Replies Last reply
    0
    • N NET India

      Hello Friends, I'm generating a xml file from sql table in asp.net(c#). Here is my code given below using (SqlConnection mCon = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString())) { mCon.Open(); string strsql = "Select * From MakeTable Where Active=1 For XML Auto,ELEMENTS"; //string strsql = "Select * From MakeTable Where Active=1 "; SqlCommand mDataCom = new SqlCommand(); mDataCom.Connection = mCon; mDataCom.CommandText = strsql; mDataCom.CommandTimeout = 15; DataSet ds = new DataSet(); XmlReader xmlReader = null; xmlReader = mDataCom.ExecuteXmlReader(); ds.ReadXml(xmlReader); ds.DataSetName = "Halo"; ds.WriteXml("F://Manoj//Autoneed/Rough//XMLFile2.xml"); } but it's giving me output as shown here <MakeTable> <MakeID>10</MakeID> <MakeNameID>36</MakeNameID> <MakeYear>2009</MakeYear> <Active>1</Active> </MakeTable> but if i run my sql command in sql then it's giving me following records as shown below <MakeTable> <MakeID>1</MakeID> <MakeNameID>17</MakeNameID> <MakeYear>2009</MakeYear> <Active>1</Active> </MakeTable> <MakeTable> <MakeID>3</MakeID> <MakeNameID>17</MakeNameID> <MakeYear>2006</MakeYear> <Active>1</Active> </MakeTable> <MakeTable> <MakeID>4</MakeID> <MakeNameID>17</MakeNameID> <MakeYear>2007</MakeYear> <Active>1</Active> </MakeTable> <MakeTable> <MakeID>5</MakeID> <MakeNameID>17</MakeNameID> <MakeYear>2005</MakeYear> <Active>1</Active> </MakeTable> <MakeTable> <MakeID>7</MakeID> <MakeNameID>17</MakeNameID> <MakeYear>2004</MakeYear> <Active>1</Active> </MakeTable> can anybody let me know how can i get this output or where and what change i should make in my code or sqlcommand

      C Offline
      C Offline
      compninja25
      wrote on last edited by
      #2

      What happens if you try this?

      while (xmlReader.Read())
      {
      ds.ReadXml(xmlReader);
      ds.DataSetName = "Halo";
      ds.WriteXml("F://Manoj//Autoneed/Rough//XMLFile2.xml");
      }

      Knowledge is not power, however, the acquisition and appropriate application of knowledge can make you a very powerful individual.

      C 1 Reply Last reply
      0
      • C compninja25

        What happens if you try this?

        while (xmlReader.Read())
        {
        ds.ReadXml(xmlReader);
        ds.DataSetName = "Halo";
        ds.WriteXml("F://Manoj//Autoneed/Rough//XMLFile2.xml");
        }

        Knowledge is not power, however, the acquisition and appropriate application of knowledge can make you a very powerful individual.

        C Offline
        C Offline
        compninja25
        wrote on last edited by
        #3

        it didn't dawn on me at first glance that you had a dataset in there...so I don't think the above would work. Since it appears to be coming back from the SQL server correctly, what if you avoided the DataSet all together and just saved it straight to a file?

          // create a writer and open the file
          TextWriter tw = new StreamWriter("output.xml");
        
        
          while (datareader.read())
          {
          // write a line of text to the file
          tw.WriteLine(datareader.ToString());
          }
        
          // close the stream
          tw.Close();
        

        Knowledge is not power, however, the acquisition and appropriate application of knowledge can make you a very powerful individual.

        1 Reply Last reply
        0
        • N NET India

          Hello Friends, I'm generating a xml file from sql table in asp.net(c#). Here is my code given below using (SqlConnection mCon = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString())) { mCon.Open(); string strsql = "Select * From MakeTable Where Active=1 For XML Auto,ELEMENTS"; //string strsql = "Select * From MakeTable Where Active=1 "; SqlCommand mDataCom = new SqlCommand(); mDataCom.Connection = mCon; mDataCom.CommandText = strsql; mDataCom.CommandTimeout = 15; DataSet ds = new DataSet(); XmlReader xmlReader = null; xmlReader = mDataCom.ExecuteXmlReader(); ds.ReadXml(xmlReader); ds.DataSetName = "Halo"; ds.WriteXml("F://Manoj//Autoneed/Rough//XMLFile2.xml"); } but it's giving me output as shown here <MakeTable> <MakeID>10</MakeID> <MakeNameID>36</MakeNameID> <MakeYear>2009</MakeYear> <Active>1</Active> </MakeTable> but if i run my sql command in sql then it's giving me following records as shown below <MakeTable> <MakeID>1</MakeID> <MakeNameID>17</MakeNameID> <MakeYear>2009</MakeYear> <Active>1</Active> </MakeTable> <MakeTable> <MakeID>3</MakeID> <MakeNameID>17</MakeNameID> <MakeYear>2006</MakeYear> <Active>1</Active> </MakeTable> <MakeTable> <MakeID>4</MakeID> <MakeNameID>17</MakeNameID> <MakeYear>2007</MakeYear> <Active>1</Active> </MakeTable> <MakeTable> <MakeID>5</MakeID> <MakeNameID>17</MakeNameID> <MakeYear>2005</MakeYear> <Active>1</Active> </MakeTable> <MakeTable> <MakeID>7</MakeID> <MakeNameID>17</MakeNameID> <MakeYear>2004</MakeYear> <Active>1</Active> </MakeTable> can anybody let me know how can i get this output or where and what change i should make in my code or sqlcommand

          W Offline
          W Offline
          www Developerof NET
          wrote on last edited by
          #4

          Is it mandatory for you to write down the file to the server????? why don`t you try

          ds.GetXml();

          But remember to fill you dataset with the data. which actually returns you a XmlDocument. Debug your code and see what results you get in the document object.

          When you fail to plan, you are planning to fail.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups