Solved

C# new line to break from XML

Posted on 2010-11-24
9
1,274 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

867 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

21 Experts available now in Live!

Get 1:1 Help Now