Solved

C# new line to break from XML

Posted on 2010-11-24
9
1,330 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to filter by key press ? 6 57
Please Explain What Reading a Zip file Back In Means 11 68
C# LINQ 5 27
Validation for Passport expiry in asp.net 2 15
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

726 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