Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Line Breaks in XML Files

Posted on 2008-10-09
4
Medium Priority
?
432 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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
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.
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

705 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