C# provides the XmlWriter class which allows developers to write to an Xml file. To begin, we need to import the system.xml namespace.
The system.xml namespace contains the XmlWriter and XmlTextWriter classes.
To read an xml file to write out, we can call the constructor of the XmlTextWriter class. This will read the xml file specified, so that it can be manipulated.
XmlTextWriter xmlWriter = new XmlTextWriter("C:\\Temp\myTestXml.xml", null);
This would create the file C:\\Temp\myTestXml.xml if it did’nt exist.
We can write a comment into the xml file using
xmlWriter.WriteComment("This is a comment in the xml file"); xmlWriter.Flush()
To write out/display the xml content in the file in a shot, we can pass Console.Out as a parameter of the constructor to the XmlTextWriter.
XmlTextWriter writer = new XmlTextWriter(Console.Out);