0

I have been looking at different methods for creating an XML document using: XmlDocument, XDocument and XmlWriter. It seems like I have to use XmlWriterSettings along with XmlWriter in order to specify the Indent and IndentChars.

This MSDN Article specifies that the default IndentChars value is Two spaces.

My question: Is it possible to save an XML file using XmlDocument or XDocument where I can specify the indent value for the saved XML file?

If it is possible to do so without using XmlWriter, can someone provide an example or documentation?

Elias Wick
  • 483
  • 6
  • 21

1 Answers1

1

Update: Sorry, I didn't read clearly your question. No, it is not possible. If you want to specify IdentChars, you should create XmlTextWriter

    var xmlDoc = new XmlDocument();

    // your logic 


    using (var writer = new XmlTextWriter("filename.xml")) {
        writer.Indentation = 1;
        writer.IndentChar = ' ';

        xmlDoc.Save(writer);
    }

Anton
  • 801
  • 4
  • 10
  • 1
    And where are you specifying the indent value? – Heretic Monkey Apr 02 '20 at 18:01
  • I don't think you understand the question. I am looking for a method where I can specify the indent value before saving the file. Using your code would save the file with only two spaces as the indent value. How can I save the file with four spaces as the indent value without using `XmlWriter`? – Elias Wick Apr 02 '20 at 18:02
  • 1
    @Anton, thank you for changing your answer. I recently read that `XmlTextWriter` isn't the optimal way for changing the IndentChars, they recommend using `XmlWriter`. Here is a link to the source: https://stackoverflow.com/a/6899460/7556093 – Elias Wick Apr 02 '20 at 18:11
  • However, I will accept your answer as the correct answer unless someone knows a solution within the day. – Elias Wick Apr 02 '20 at 18:12
  • @Elias Wick Agreed. – Anton Apr 02 '20 at 18:26