user3354792 user3354792 - 3 years ago 159
C# Question

Remove all Indentation

I have an XML that looks like this:

<DataRow>
<Name><![CDATA[DateAdd]]></Name>
<Description><![CDATA[Adds or subtracts some interval of time from a date or time.]]></Description>
<Syntax><![CDATA[DateAdd(interval, number, date)]]></Syntax>
<Notes><![CDATA[Interval may be:
yyyy=year
q=quarter
m=month
y=day of year
d=day
w=weekday
ww=week of year
h=hour
n=minute
s=second]]>
</Notes>
</DataRow>


I want to remove all indentation even inside the
CDATA
. It is easy to add indentation using
XmlTextWriter
but I cannot find anything to remove indents.

Answer Source

Since you want to remove all indentation, you don't need an XML parser/writer.

You can load the text as separate lines using File.ReadAllLines, and then process each one of them with string.TrimStart. Then save them back to disk using File.WriteAllLines.

N.B.: File.WriteAllLines appends a newline at the end of the file (since it follows each line by a newline); if you don't want one, you should join the lines yourself.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download