0

I am doing a CRUD of students and I am saving each of them in an XML file. The case is that when saving them, they are saved in the same line.

The code is this:

using(StreamWriter sr = File.AppendText(Xml))
using(XmlTextWriter xml = new XmlTextWriter(sr))
{
     xml.WriteElementString(Xml, dataStudent);
}

And the output looks like this:

<.\student.xml>b4f6d4ca-ba74-4f33-9481-a24e3efffcfa, 54, Deni, Suco, 12/6/1972</.\student.xml><.\student.xml>cf98fab5-5b13-4c4c-849c-ebbb1c7157ab, 56, Sandy, Whoknows, 12/8/79</.\student.xml>

And I want that:

<.\student.xml>b4f6d4ca-ba74-4f33-9481-a24e3efffcfa, 54, Deni, Suco, 12/6/1972</.\student.xml>
<.\student.xml>cf98fab5-5b13-4c4c-849c-ebbb1c7157ab, 56, Sandy, Whoknows, 12/8/79</.\student.xml>
  • Investigate [`XmlWriterSettings`](https://learn.microsoft.com/en-us/dotnet/api/system.xml.xmlwritersettings.indent?view=netframework-4.7.2). – Dour High Arch Feb 17 '19 at 19:56
  • 1
    you should first rethink your xml structure ...element `<.\student.xml>` looks awkward ... next: `b4f6d4ca-ba74-4f33-9481-a24e3efffcfa, 54, Deni, Suco, 12/6/1972` looks even mor awkward ... it shoudl be rather `` or `b4f6d4ca-ba74-4f33-9481-a24e3efffcfa54....` – Selvin Feb 17 '19 at 20:09

1 Answers1

0

You can use the following property of XmlTextWriter.

xml.Formatting = Formatting.Indented;
xml.Indentation = 4;

Here is the link https://learn.microsoft.com/en-us/dotnet/api/system.xml.xmltextwriter.indentation?view=netframework-4.7.2

kapd
  • 639
  • 1
  • 7
  • 20