Improve company productivity with a Business Account.Sign Up


C# new line to break from XML

Posted on 2010-11-24
Medium Priority
Last Modified: 2012-05-10

I have one XML document.
I open it with C# LINQ to XML

when I try to replace "\r\n", "\r" or "\n" to "<br />" it is not working.

Some help please.
XDocument xmldata = XDocument.Load(Server.MapPath("content.xml"));

        var quotes = from quote in xmldata.Descendants("row")
                     where (int)quote.Attribute("ID") == 1
                     select new
                         Content = (string)quote.Attribute("content"),

        foreach (var quote in quotes)
            content = quote.Content;

        content = content.Replace("\r\n", "<br />").Replace("\n", "<br />").Replace("\r", "<br />");

Open in new window

Question by:kuzmar
  • 4
  • 3
  • 2

Expert Comment

ID: 34204650
Can you paste or attach an example of Content.xml?

Author Comment

ID: 34204733
Here it is:
LVL 21

Expert Comment

by:Alfred A.
ID: 34204746
You can try this:

content = content.Replace(System.Environment.NewLine, "<br />");
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

LVL 21

Expert Comment

by:Alfred A.
ID: 34204768
Try the following as well:

string content = Regex.Replace(content, @"\r\n", "<br />",RegexOptions.Multiline);

content = Regex.Replace(content, @"\r", "<br />",RegexOptions.Multiline);

content = Regex.Replace(content, @"\n", "<br />",RegexOptions.Multiline);
LVL 21

Expert Comment

by:Alfred A.
ID: 34204776
Oh I forgot to mention in my previous post that you need to import the System.Text.RegularExpressions library.
LVL 21

Expert Comment

by:Alfred A.
ID: 34204820
You can also try the following:

content = content.Replace("" + (char) 13 + (char) 10, "<br />");

content = content.Replace("" + (char) 13, "<br />");

content = content.Replace("" + (char) 10, "<br />");

Accepted Solution

buraksarica earned 2000 total points
ID: 34204882
I see. Unfortunately, you cannot preserve newline chars in the attribute values. if you are able to produce this xml in another format, (i mean if YOU are producing this xml) you can wrap the content in an element directly such as:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<row ID='1'>&quot;Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam tempor pretium.  
Aliquam felis quam, sagittis id egestas at, vulputate ut elit.
Nam nec metus libero. Suspendisse sagittis justo at nisi lobortis aliquam&quot;</row>
<row ID='2'>&quot;Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam tempor pretium.
Aliquam felis quam, sagittis id egestas at, vulputate ut elit.&quot;</row>

Open in new window

Then you can use PreserveWhitespace option of XDocument load method:

XDocument xmldata = XDocument.Load(Server.MapPath("content.xml"),LoadOptions.PreserveWhitespace);

Open in new window


Expert Comment

ID: 34204895
Of course, Content = (string)quote.Attribute("content") this line will change to
Content = (string)quote.Value;

Author Closing Comment

ID: 34205345
Yes. I would have to change how the xml is being presnted. Put content in an element directly.


Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
An ASP.NET Web Form User Control is not newly introduced in ASP.NET. In fact, it was an old technology yet still playing a role to generate web content, especially when we want to use it to have a better and easy way to control part of the web conte…
When you have multiple client accounts to manage, it often feels like there aren’t enough hours in the day. With too many applications to juggle, you can’t focus on your clients, much less your growing to-do list. But that doesn’t have to be the cas…
Watch the working video to know how to import Outlook PST/OST files to Amazon WorkMail. Kernel released this tool which is very easy to use and migrate single or multiple PST and OST files to Amazon WorkMail. To know more about Kernel Import PST to …

608 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