Solved

Format XmlDocument newline

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

920 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

13 Experts available now in Live!

Get 1:1 Help Now