I am saving xml from .NET's XElement. I've been using the method ToString, but the formatting doesn't look how I'd like (examples below). I'd like at most two tags per line. How can I achieve that?


Saving XElement.Parse("<a><b><c>one</c><c>two</c></b><b>three<c>four</c><c>five</c></b></a>").ToString() gives me

<a>
  <b>
    <c>one</c>
    <c>two</c>
  </b>
  <b>three<c>four</c><c>five</c></b>
</a>

But for readability I would rather 'three', 'four' and 'five' were on separate lines:

<a>
  <b>
    <c>one</c>
    <c>two</c>
  </b>
  <b>three
    <c>four</c>
    <c>five</c>
  </b>
</a>

Edit: Yes I understand this is syntactically different and "not in the spirit of xml", but I'm being pragmatic. Recently I've seen megabyte-size xml files with as few as 3 lines—these are challenging to text editors, source control, and diff tools. Something needs to be done! I've tested that changing the formatting above is compatible with our application.

Edit
Report