0

So, I'm building a windows forms application that uses a StreamReader/StreamWriter to read each line of the .aspx, .ascx and .master pages on our asp.net website. It then removes certain properties and such from the controls through string manipulation, and writes the result back (overriding the page's markup with the edited markup). The problem is some of these pages are being written as one continuous line.

I've been unable to find anyway to call the visual studios 'Format Document' function. I found this question that would likely accomplish my goal if I weren't trying to do this from my Windows Form Application (as it's an automated process).

Any tips or points in the right direction would be appreciated.

Community
  • 1
  • 1
Jmnstr
  • 80
  • 13

1 Answers1

1

A quick-and-dirty-solution would be (and I don't recommend it):

content = content.Replace("></", ">></").Replace("><", ">\n\t<").Replace(">></", "></");

content is the string that holds the web content.

First and last replacements are to avoid the second replacement to add newlines between something like this <tag></tag>. The above code of course has some flaws. Something like <tag1><tag2 /></tag1> will not be formatted correctly. You could avoid this by pre-replacing /></ with something you can safely re-replace at the end.

You may also want to replace \n with \r\n perhaps.

Robert S.
  • 1,942
  • 16
  • 22
  • Hey Robert, Thanks for the answer. Sadly, I've tried a similar method and found that I too wouldn't recommend it. Although it's starting to look like the route I'll need to take. – Jmnstr May 20 '15 at 21:35
  • 1
    If your data is valid xml you also can use something like `System.Xml.XmlDocument`, load your content as an XML document and save it with identation. See `System.Xml.XmlTextWriter` for example. This class provides the properties `Indentation` and `Formatting`. If your data is nearly valid XML you also can try to load it as an XML fragment, disable validation and so on. – Robert S. May 20 '15 at 21:39