Hanna
Hanna

Reputation: 10753

Saving a table to an XML file

I am currently working on a website. I have been asked to be able to allow admins to save tables in the database to an XML file (so they can download it from their browser).

I don't even know where to begin. I've done multiple hours of research but this process doesn't seem very well documented. I would appreciate some pointers in the right direction, or if possible, a more in-depth explanation.

How would I set-up my files? How can I make them download that file instead of saving it into my application? How would I even save it to an XML file to start?

I've seen some stuff online about backing your database into an XML file, but I feel that this is different as it allows users to download a database file instead of the developer making one.

EDIT

For clarity's sake the XML should resemble the database. So, if I have a Product (or products), I would hope the XML looks something similar to the following:

<product>
     <title>
          My Product
     </title>
     <price>
          5.00
     </price>
     <quantity>
          4
     </quantity>
     <description>
          This is my product!
     </description>
</product>

Upvotes: 1

Views: 1740

Answers (1)

Aaron Bertrand
Aaron Bertrand

Reputation: 280262

The query you might want is:

SELECT col1, col2, col3 etc.
  FROM dbo.TableName
  FOR XML AUTO, ELEMENTS;

Run that in Management Studio to see what it returns. I think you need to split your question into multiple parts. This one is how you generate XML from a table. A different question will be how to consume that from C# and save it to a file. These questions might give you a head start, but I don't know if you will need to do anything differently in MVC3 (I know how to spell it, but not much else):

How to save this string into XML file?

Save xml string or XmlNode to text file in indent format?

Upvotes: 5

Related Questions