Solved

Format XmlDocument newline

Posted on 2007-03-20
3
1,125 Views
Last Modified: 2008-01-09
Hi Experts,

My XmlDocument doesn't include any NewLine char.
So when I use:
System.Diagnostics.Debug.WriteLine(xmlDocument.OuterXml);
Then the text is written on 1 line.

I want the same XmlDocument to be formatted and displayed on several lines (each node on 1 line).

I don't want to rewrite all the XmlDocument: I want to format it at the end of the building.

Thanks in advance for your help.
0
Comment
Question by:noulouk
  • 2
3 Comments
 
LVL 16

Expert Comment

by:jimbobmcgee
ID: 18759969
If its just for presentation (into a text box, for example), maybe a Regular Expression replace might help to start.  Something like:

    private void TryMe()
    {
        Regex regEx = new Regex("</.+?>", RegexOptions.Multiline | RegexOptions.ECMAScript);
        string outXml = regEx.Replace(xmlDoc.OuterXml, new MatchEvaluator(InsertNewLine));
        Debug.WriteLine(outXml);
    }

    private string InsertNewLine(Match m)
    {
        return string.Format("{0}{1}", m, Environment.NewLine);
    }

HTH

J.
0
 
LVL 9

Author Comment

by:noulouk
ID: 18760134
Thanks for this quick answer jimbobmcgee.

Your code achieve a part of the job, so we are near the final solution.
I get this formatting:
<item><subitem1></subitem1>
<subitem2></subitem2>
<subitem3> </subitem3>
<subitem4></subitem4>
</item>

I'd like this:
<item>
<subitem1></subitem1>
<subitem2></subitem2>
<subitem3> </subitem3>
<subitem4></subitem4>
</item>

And my first line is something like this,  so no formatting:
<?xml version="1.0" encoding="utf-8"?><?xml-stylesheet type='text/xsl' href='/rss.xsl' version='1.0'?><!--description--><rss version="2.0"><channel><title>test</title>
0
 
LVL 16

Accepted Solution

by:
jimbobmcgee earned 500 total points
ID: 18764721
Don't think you'll get it in one RegEx, so maybe three might do it:

    private void TryMe()
    {
        Regex regExDecs = new Regex("(?'decs'<(\?)(.+?)/title>)(?'rest'.*)", RegexOptions.Multiline | RegexOptions.ECMAScript);
        Regex regExRoot = new Regex("(?'root'<[\w]*>)[\s]*(?'first'<[\w]*>)", RegexOptions.Multiline | RegexOptions.ECMAScript);
        Regex regExOthers = new Regex("</.+?>", RegexOptions.Multiline | RegexOptions.ECMAScript);

        Match decsMatch = reExDecs.Match(xmlDoc.OuterXml);
        string outXml = decsMatch.Groups[4].Value;
        outXml = regExRoot.Replace(xmlDoc.OuterXml, new MatchEvaluator(InsertRootNewLine));
        outXml = regExOthers.Replace(outXml, new MatchEvaluator(InsertNewLine));

        Debug.WriteLine(outXml);
    }

    private string InsertRootNewLine(Match m)
    {
        return string.Format("{0}{1}{2}", m.Groups[1].Value, Environment.NewLine, m.Groups[2].Value);
    }

    private string InsertNewLine(Match m)
    {
        return string.Format("{0}{1}", m, Environment.NewLine);
    }

J.
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

816 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

12 Experts available now in Live!

Get 1:1 Help Now