Solved

Line Breaks in XML Files

Posted on 2008-10-09
4
430 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
[X]
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
  • 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 30

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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

623 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