Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Line Breaks in XML Files

Posted on 2008-10-09
4
428 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

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.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used.

789 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