Solved

Format XmlDocument newline

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

Technology Partners: 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!

Question has a verified solution.

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

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

738 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