Click here to Skip to main content
Click here to Skip to main content

Tagged as

C# - XML Writer Settings - Adding Line breaks/indentation

, 27 Sep 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
When dynamically creating XML documents, they appear just like a normal text file. This tip is to add line breaks and indentation automatically (I posted this in my blog a while ago)
I have seen that at times, we need to generate XML using code. This is in contrast to web services, wherein XML is generated by the compiler. In this post, I would like to give some pointers about creating XML files using C# - the "XmlWriter" class. Consider the following piece of code:
 
XmlWriter xml = XmlWriter.Create(@"c:\file.xml");
 
This would instantiate an XML writer and you could use various commands like xml.WriteStartElement(...), xml.WriteValue(..), xml.WriteEndElement() to construct the XML file dynamically. But there is a problem with this. A file created with such an instance of XmlWriter (XML) would look like this:
 
<?xml version="1.0" encoding="utf-8"?><Users><User><Name>user1</Name></User><User><Name>user2</Name></User><User><Name>user3</Name></User></Users>
 
But, we may expect the XML file to have some line breaks, indentation to make it readable to others. For this, we need to apply some XML writer settings using the "XmlWriterSettings" class as shown below:
 
XmlWriterSettings xmlWriterSettings = new XmlWriterSettings();
xmlWriterSettings.NewLineOnAttributes = true;
xmlWriterSettings.Indent = true;
 
Now the XmlWriter would be instantiated as:
 
XmlWriter xml = XmlWriter.Create(path, xmlWriterSettings);
 
With this, the XML generated would be indented, with line breaks as appropriate! Check out the final output after the additional settings:
 
<?xml version="1.0" encoding="utf-8"?>
<Users>
    <User>
  &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;<Name>user1</Name>
 &nbsp;&nbsp;&nbsp;</User>
 &nbsp;&nbsp;&nbsp;<User>
  &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;<Name>user2</Name>
 &nbsp;&nbsp;&nbsp;</User>
 &nbsp;&nbsp;&nbsp;<User>
  &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;<Name>user3</Name>
 &nbsp;&nbsp;&nbsp;</User>
</Users>

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Karthik. A
Software Developer
United States United States
Just another passionate software developer! I recently acquired my MCTS Wink | ;)
 
My latest contribution to the open source world is a blog engine written in MVC 3 - sBlog.Net. Check it out here. For the codeproject article regarding sBlog.Net click here!
 
I have recently (re)launched my web-site. Check it out at http://thekfactor.info!

Eventually, I have a blog under my own sub-domain. It is http://blog.thekfactor.info

Also check out my latest app - directory comparer [codeproject article and dedicated site] and my latest jquery plugin - partial collapsible panel!!!


All sites were developed in ASP.Net MVC 2 Smile | :)

Follow on   Twitter

Comments and Discussions

 
GeneralReason for my vote of 3 nice PinmemberSaranRam4-Oct-10 22:34 
GeneralReason for my vote of 5 Wow! I definitely didn't know that! PinmemberSimon Dufour28-Sep-10 7:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.1411023.1 | Last Updated 27 Sep 2010
Article Copyright 2010 by Karthik. A
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid