Solved

C# new line to break from XML

Posted on 2010-11-24
9
1,373 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

734 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