Solved

Line Breaks in XML Files

Posted on 2008-10-09
4
424 Views
Last Modified: 2012-05-05
How do I get a line break or carriage retunr in the actual XML file so it does not go across one line. I have tried the XMLWriterSettings but it does not appear to work. The code is as below:

ublic partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        // location to the XML file to write
        String strFilePath = "c:\\Projects\\company\\App_Data\\movies.xml";

        // create an instance of the XmlTextWriter object
        XmlTextWriter objWriter = new XmlTextWriter(strFilePath, System.Text.Encoding.UTF8);

        XmlWriterSettings settings = new XmlWriterSettings();
        settings.Indent = true;
        settings.OmitXmlDeclaration = true;
        settings.NewLineOnAttributes = true;

        objWriter.WriteStartElement("order");
        objWriter.WriteAttributeString("orderID", "367A54");
        objWriter.WriteAttributeString("date", "2001-05-03");
        objWriter.WriteElementString("price", "19.95");
        objWriter.WriteEndElement();

        objWriter.Flush();

        // clear up memory
         objWriter.Close();

         // successfully done
         result.Text = "XML file ";
         
    }
}
0
Comment
Question by:MangoMarcus
  • 2
4 Comments
 
LVL 12

Accepted Solution

by:
AshleyBryant earned 500 total points
ID: 22682314
Have you tried this?
objWriter.WriteWhitespace("\n");

Open in new window

0
 
LVL 29

Expert Comment

by:anarki_jimbel
ID: 22683228
I think the problem is that you haven't set your xmlWriterSettings...

You need to create settings before creating a writer and pass this settings object to a writer constructor,

something like


        XmlWriterSettings settings = new XmlWriterSettings();
        settings.Indent = true;
        settings.OmitXmlDeclaration = true;
        settings.NewLineOnAttributes = true;


XmlTextWriter writer   = XmlTextWriter.Create("books.xml", settings);
0
 

Author Closing Comment

by:MangoMarcus
ID: 31504668
Cheers Ashley, worked a treat!

(Thanks Anarki - its seems to throw an error when you swap (strFilePath, System.Text.Encoding.UTF8) for (strFilePath, System.settings) - obviously something im doing wrong!)
0
 

Author Comment

by:MangoMarcus
ID: 22685214
Sorry Anarki, I meant (strFilePath, settings)
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now