user3354792
user3354792

Reputation: 13

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.

Upvotes: 1

Views: 3258

Answers (4)

hunty
hunty

Reputation: 1

I suggest this may help:

string markup =
@"<Root>
    <Child>
        <GrandChild/>
    </Child>
</Root>";

XmlReader nodeReader = XmlReader.Create(new StringReader(markup));
nodeReader.MoveToContent();

XElement xRoot = XElement.Load(nodeReader);
var result = xRoot.ToString(SaveOptions.DisableFormatting);

result will contain unindented string.

Upvotes: 0

default
default

Reputation: 11635

I agree with the previous posters. An example of that would be the following

string path = "path_to_file";
IEnumerable<string> lines = File.ReadAllLines(path);

File.WriteAllLines(path, lines.Select(line => line.TrimStart()));

This will remove all the leading white spaces from all the lines in the file.

Upvotes: 1

Matthew Walton
Matthew Walton

Reputation: 9959

The easiest way is to forget that it's XML. Read the file in, split it into lines, trim the whitespace off each line and write the file back out again. The File.ReadLines and String.TrimStart methods are likely to be useful for that.

This avoids any issues with XML parsers preserving layout (although you can save an XDocument telling it to disable formatting), but also would remove the indentation from the CDATA section, which an XML parser won't touch because CDATA is explicitly defined as being just a string, which the XML parser cannot interpret in any way.

So it seems like a slightly odd requirement, not really in keeping with how XML is usually handled, and therefore I'd suggest not treating it like XML.

Upvotes: 0

Solal Pirelli
Solal Pirelli

Reputation: 1219

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.

Upvotes: 4

Related Questions