Solved

Format XmlDocument newline

Posted on 2007-03-20
3
1,105 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
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.…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

707 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

15 Experts available now in Live!

Get 1:1 Help Now