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 8965009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:43:07+00:00 2026-06-15T16:43:07+00:00

Posting both question and (one) answer here to see if somebody has a better

  • 0

Posting both question and (one) answer here to see if somebody has a better solution…

I am trying to write formatted XML output from stored procedure to file. My first attempt looked like this:

// the actual command is a stored procedure that returns an XML document, but use this for demo purposes
var xmlCommand = new SqlCommand("SELECT 1 as ID, 'asdf' as Name FOR XML PATH, ROOT('MyData')", conn);
File.WriteAllText("file1.xml", (string)xmlCommand.ExecuteScalar());

Which was nice and short, but the file generated has everything stuffed on one line:

<MyData><row><ID>1</ID><Name>asdf</Name></row></MyData>

I want it to be formatted nicely:

<MyData>
  <row>
    <ID>1</ID>
    <Name>asdf</Name>
  </row>
</MyData>
  • 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-06-15T16:43:08+00:00Added an answer on June 15, 2026 at 4:43 pm

    One approach is to use an XMLDocument and an XMLTextWriter.

    using (XmlReader reader = xmlCommand.ExecuteXmlReader())
    {
        XmlDocument dom = new XmlDocument();
        dom.Load(reader);
    
        var settings = new XmlWriterSettings();
        settings.Indent = true;
        settings.OmitXmlDeclaration = true;
    
        using (var writer = XmlTextWriter.Create("file2.xml", settings))
        {
            dom.WriteContentTo(writer);
        }
    }
    

    It gets the job done but it seems like a lot of code to accomplish a simple task.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just posting the solution I worked out today. See my answer below. If you
Well I couldn't find any previous posting to answer my question so.... I am
Installed both SonataMediaBundle, SonataNewsBundle and SonataAdminBundle (and much more but these matter). When posting
Posting this one for a friend. They have an Icefaces app that uses Icefaces's
I have considered posting this question on GameDev since my case relates to a
hello i'm a beginner java developer and this is one question in a list
Posting a stack overflow question on stackoverflow.com, how amusing :-) I'm running some recursive
Found the answer here (last post): http://social.msdn.microsoft.com/Forums/eu/wcf/thread/f5c0ea22-1d45-484e-b2c0-e3bc9de20915 I'm having one last issue with the
I am working on a product which has both a desktop web site and
I did search before posting this question and didn't find anything directly related, so

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.