Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3240530
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T18:05:45+00:00 2026-05-17T18:05:45+00:00

Let’s say I have a table Employee like this EmpID, EmpName 1 , hatem

  • 0

Let’s say I have a table Employee like this

EmpID, EmpName

1    , hatem

and I write a query: select * from Employee for xml auto

so the output will be in XML format.

I want to know how can I export the result to a XML file to be saved on my computer’s drive as I need to read the XML files from this folder and deserialize them in my .net application.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T18:05:46+00:00Added an answer on May 17, 2026 at 6:05 pm

    If you only need to store the XML and not do anything else to it, this is probably the easiest way to accomplish this – using straight simple ADO.NET:

    string query = "SELECT EmployeeID, LastName, FirstName, Title, BirthDate, HireDate FROM dbo.Employees FOR XML AUTO";
    
    using(SqlConnection _con = new SqlConnection("server=(local);database=Northwind;integrated security=SSPI;"))
    using (SqlCommand _cmd = new SqlCommand(query, _con))
    {
        _con.Open();
        string result = _cmd.ExecuteScalar().ToString();
        _con.Close();
    
        File.WriteAllText(@"D:\test.xml", result);
    }
    

    This will create a file D:\test.xml (or change that to match your system) and will put those XML tags into that file.

    The SqlCommand object also has a .ExecuteXmlReader() method which would return an XmlReader object to scan and manipulate the XML – not just return a string. Use whatever makes the most sense to you!

    PS: also, the output of FOR XML AUTO is a bit …. let’s say … suboptimal. It uses the dbo.Employee as it’s main XML tag and so forth… with SQL Server 2008, I would strongly recommend you look into using FOR XML PATH instead – it allows you to tweak and customize the layout of the XML output.

    Compare your original XML output with FOR XML AUTO

    <dbo.Employees _x0040_ID="1" LastName="Davolio" FirstName="Nancy" Title="Sales Representative" BirthDate="1948-12-08T00:00:00" HireDate="1992-05-01T00:00:00" />
    <dbo.Employees _x0040_ID="2" LastName="Fuller" FirstName="Andrew" Title="Vice President, Sales" BirthDate="1952-02-19T00:00:00" HireDate="1992-08-14T00:00:00" />
    

    against this query – just to see the difference:

    SELECT 
        [EmployeeID] AS '@ID',
        [LastName], [FirstName],
        [Title],
        [BirthDate], [HireDate]
    FROM 
        [dbo].[Employees]
    FOR XML PATH('Employee'), ROOT('Employees')
    

    Output is:

    <Employees>
      <Employee ID="1">
        <LastName>Davolio</LastName>
        <FirstName>Nancy</FirstName>
        <Title>Sales Representative</Title>
        <BirthDate>1948-12-08T00:00:00</BirthDate>
        <HireDate>1992-05-01T00:00:00</HireDate>
      </Employee>
      <Employee ID="2">
        <LastName>Fuller</LastName>
        <FirstName>Andrew</FirstName>
        <Title>Vice President, Sales</Title>
        <BirthDate>1952-02-19T00:00:00</BirthDate>
        <HireDate>1992-08-14T00:00:00</HireDate>
      </Employee>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have table with column 'URL' whrere I store urls like this
Let’s say I have a number like 0x448 . In binary this is 0100
Let's say I have a text file composed like this ##### typeofthread1 ##### typeofthread2
Let's say I have a list of objects TheListOfObjects. If I write this: TheListOfObjects
Let's say that I have a set of relations that looks like this: relations
Let's say I have this MySQL table: OK.. see the type field? Type 0
Let's say on a page I have alot of this repeated: <div class=entry> <h4>Magic:</h4>
Let's say I can call a method like this: core::get() . What is the
Let's say I have the following text: (example) <table> <tr> <td> <span>col1</span> </td> <td>col2</td>
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.