Link to home
Start Free TrialLog in
Avatar of noulouk
noulouk

asked on

Format XmlDocument newline

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.
Avatar of jimbobmcgee
jimbobmcgee
Flag of United Kingdom of Great Britain and Northern Ireland image

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.
Avatar of noulouk
noulouk

ASKER

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>
ASKER CERTIFIED SOLUTION
Avatar of jimbobmcgee
jimbobmcgee
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial