Indentation and new line command for XMLwriter in C#

Use a XmlTextWriter instead of XmlWriter and then set the Indentation properties. Example string filename = “MyFile.xml”; using (FileStream fileStream = new FileStream(filename, FileMode.Create)) using (StreamWriter sw = new StreamWriter(fileStream)) using (XmlTextWriter xmlWriter = new XmlTextWriter(sw)) { xmlWriter.Formatting = Formatting.Indented; xmlWriter.Indentation = 4; // … Write elements } Following @jumbo comment, this could also be … Read more

How to put an encoding attribute to xml other that utf-16 with XmlWriter?

You need to use a StringWriter with the appropriate encoding. Unfortunately StringWriter doesn’t let you specify the encoding directly, so you need a class like this: public sealed class StringWriterWithEncoding : StringWriter { private readonly Encoding encoding; public StringWriterWithEncoding (Encoding encoding) { this.encoding = encoding; } public override Encoding Encoding { get { return encoding; … Read more

How to create a XmlDocument using XmlWriter in .NET?

Here’s at least one solution: XmlDocument doc = new XmlDocument(); using (XmlWriter writer = doc.CreateNavigator().AppendChild()) { // Do this directly     writer.WriteStartDocument();     writer.WriteStartElement(“root”);     writer.WriteElementString(“foo”, “bar”);     writer.WriteEndElement();     writer.WriteEndDocument(); // or anything else you want to with writer, like calling functions etc. } Apparently XpathNavigator gives you a … Read more

Create XML in JavaScript

Disclaimer: The following answer assumes that you are using the JavaScript environment of a web browser. JavaScript handles XML with ‘XML DOM objects’. You can obtain such an object in three ways: 1. Creating a new XML DOM object var xmlDoc = document.implementation.createDocument(null, “books”); The first argument can contain the namespace URI of the document … Read more

tech