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?

1 Answer 1

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);
    }

Sign up to request clarification or add additional context in comments.

5 Comments

And where are you specifying the indent value?
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?
@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: stackoverflow.com/a/6899460/7556093
However, I will accept your answer as the correct answer unless someone knows a solution within the day.
@Elias Wick Agreed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.