Solved

C# new line to break from XML

Posted on 2010-11-24
9
1,263 Views
Last Modified: 2012-05-10
Hi!

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

0
Comment
Question by:kuzmar
  • 4
  • 3
  • 2
9 Comments
 
LVL 5

Expert Comment

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

Author Comment

by:kuzmar
ID: 34204733
Here it is:
content.xml
0
 
LVL 21

Expert Comment

by:Alfred1
ID: 34204746
You can try this:

content = content.Replace(System.Environment.NewLine, "<br />");
0
 
LVL 21

Expert Comment

by:Alfred1
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);
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 21

Expert Comment

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

Expert Comment

by:Alfred1
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 />");
0
 
LVL 5

Accepted Solution

by:
buraksarica earned 500 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"?>
<data>
<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>
</data>

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

0
 
LVL 5

Expert Comment

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

Author Closing Comment

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


thx.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

757 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now