Click here to Skip to main content
15,906,947 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to write a carriage return + line feed + tab to an xml file, how is this possible?

FileStream fs = new FileStream(_file, FileMode.Create);
            XmlTextWriter w = new XmlTextWriter(fs, null);
            w.WriteStartDocument();
            w.WriteStartElement("articles");
            w.WriteComment("This file was generated by the XmlTextWriter class");
            w.WriteChars... :confused:
Posted

You mean ControlChars.CrLf?
 
Share this answer
 
Comments
jon-80 26-Aug-10 3:15am    
No
Dalek Dave 26-Aug-10 4:13am    
Short and Accurate! :)
Environment.NewLine gives you that.
 
Share this answer
 
Comments
jon-80 25-Aug-10 19:53pm    
Environment.NewLine returns a string; I need to write CRLF after the xml end tag.
Christian Graus 26-Aug-10 3:21am    
No, it does not. It returns a newline character, which is CRLF.
AspDotNetDev 27-Aug-10 14:48pm    
Environment.NewLine is a string. And CRLF is actually two characters (carriage return and line feed). Pretty sure Environment.NewLine may be different depending on the operating system (e.g., with Mono implementations of .Net), so that is not necessarily correct anyway.
Christian Graus 27-Aug-10 15:27pm    
That is, in fact, kind of the point. One would assume that he wants CRLF because of the operating system he is under, it would return the correct newline for whatever OS he wants. Environment.NewLine is a string because it has two characters, which is what he asked for, I did say 'newline character', but I meant whatever characters constitute a newline in that OS. I see what you mean, the OP DID say 'it returns a string' and I said 'no, it does not'. I should have said 'CRLF IS a string'.
jon-80 27-Aug-10 16:14pm    
Thanks for the explanation. Do you think I'd be able to convert Environment.NewLine to a char[], because that's what I would require if I were to use Environment.NewLine. If yes, how?
I found that adding the following lines formats my xml file:
 w.Formatting = Formatting.Indented;
w.Indentation = 5;

Complete code snippet for writing to an XML file (C#)
C#
using System;
using System.Xml;
using System.IO;
namespace WriteToXML
{
    class Program
    {
        static void Main(string[] args)
        {
            string _path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
            _path = _path.Substring(6, _path.Length - 6);
            string _file =  _path + "\\article.xml";
            FileStream fs = new FileStream(_file, FileMode.Create);
            XmlTextWriter w = new XmlTextWriter(fs, null);
            w.Formatting = Formatting.Indented;
            w.Indentation = 5;
            w.WriteStartDocument();
            w.WriteStartElement("articles");
            w.WriteComment("This file was generated by the XmlTextWriter class");
            //write content
            w.WriteStartElement("article");
            w.WriteStartElement("timestamp");
            w.WriteString(DateTime.Now.ToString());
            w.WriteEndElement();
            w.WriteStartElement("subject");
            w.WriteString("subject");
            w.WriteEndElement();
            w.WriteStartElement("body");
            w.WriteString("This is a test");
            w.WriteEndElement();
            w.WriteStartElement("entryID");
            w.WriteEndElement();
            w.WriteStartElement("deleted");
            w.WriteString("No");
            w.WriteEndElement();
            //close root element
            w.WriteEndElement();
            w.WriteEndDocument();
            w.Close();
        }
    }
}

NOTE:
1. Matthew MacDonald, Beginning ASP.NET 3.5 in C# 2008 2nd Edition
Publ. Apress. ISBN-13 978-1-59059-891-7. P. 647 to P.688
 
Share this answer
 
v2
Comments
Christian Graus 26-Aug-10 3:22am    
Yeah, I knew that, but it didn't directly answer your question, so I didn't say so.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900