Solved

Format XmlDocument newline

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

Enroll in July's Course of the Month

July's Course of the Month is now available! Enroll to learn HTML5 and prepare for certification. It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

615 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