Click here to Skip to main content
15,899,475 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all

Is there Any way to preserve line breaks in xml?

am having data(string) that contain line breaks and other informations.and i need to preserve that.but When i converted into xml it lost all line breaks.

Is there any possible way to do so?

Am using "XMLDOCUMENT.loadXml" method to load string to xml."PreserveWhitespace" property doesn't feed the requirement.
Posted

Mean the \n?

XML
XmlDocument doc = new XmlDocument();
doc.LoadXml("<xml>This my first string \\n This is my second</xml>");
MessageBox.Show(doc.OuterXml.ToString());


Actually the new line chars are not lost. While converting xml it apply it and print a second line. Instead if you want to display it , then need to escape it
 
Share this answer
 
v2
Comments
Nithin kd 24-Feb-11 7:23am    
thank you for your reply.now it is clear.
Check this link.

http://msdn.microsoft.com/en-us/library/ms256097.aspx[^]

I think you need to set the line break to \r\n to see an actual line break in your data.
 
Share this answer
 
StringBuilder outstr = new StringBuilder();

XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent=true;
settings.NewLineChars = Environment.NewLine;
settings.NewLineHandling = NewLineHandling.Replace;

XmlWriter w = XmlWriter.Create(outstr, settings);

XmlDocument doc = new XmlDocument();
doc.LoadXml(xmlString);
doc.WriteTo(w);
string here_is_a_formatted_xml_string = outstr.ToString();
 
Share this answer
 

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