- This topic helps how to create XML file using C# programming.
- For creating a new XML file in C#, we are using XmlTextWriter class.
- You have to import two NameSpaces: System.Xml, System.IO
XML (Extensible Markup Language) – How to create XML file using C#
- Extensible Markup Language (XML) is a markup language created to structure, store, and transport data by defining a set of rules for encoding documents in a format that is both human-readable and machine-readable.
- The design goals of XML emphasize simplicity, generality, and usability over the Internet.
- XML is a textual data format with strong support via Unicode for the languages of the world.
- XML is developed by World Wide Web Consortium.
- Hundreds of XML-based languages have been developed, including RSS, Atom, SOAP, and XHTML.
XmlTextWriter Class – How to create XML file using C#
- XmlTextWriter Class represents a writer that provides a fast, non-cached, forward-only way of generating streams or files containing XML data that conforms to the W3C Extensible Markup Language (XML) 1.0 and the Namespaces in XML recommendations.
Namespace: System.Xml
Assembly: System.Xml (in System.Xml.dll)
- Syntax for XmlTextWriter Class
|
1 2 3 |
// Declaration public class XmlTextWriter : XmlWriter { } |
XmlTextWriter Constructor (String, Encoding) – How to create XML file using C#
- Create an instance of the XmlTextWriter class using the specified file.
- Syntax for XmlTextWriter Constructor
|
1 2 3 |
// Declaration public DummyClass(string filename, Encoding encoding) { } |
XmlTextWriter.WriteStartDocument Method (Boolean) – How to create XML file using C#
- Write the XML declaration with the version “1.0″ and the standalone attribute.
- Syntax
|
1 2 3 |
// Declaration public override void WriteStartDocument(bool standalone) { } |
XmlWriter.WriteStartElement Method (String) – How to create XML file using C#
- When overridden in a derived class, writes out a start tag with the specified local name.
- Syntax
|
1 2 3 |
// Declaration public void WriteStartElement(string localName) { } |
How to create XML file using C# – Complete Code
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Xml; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { XmlTextWriter writer = new XmlTextWriter("C:\\productXML.xml", System.Text.Encoding.UTF8); writer.WriteStartDocument(true); writer.Formatting = Formatting.Indented; writer.Indentation = 2; writer.WriteStartElement("Table"); createNode("1", "Product 1", "1000", writer); createNode("2", "Product 2", "2000", writer); createNode("3", "Product 3", "3000", writer); createNode("4", "Product 4", "4000", writer); writer.WriteEndElement(); writer.WriteEndDocument(); writer.Close(); } public void createNode(string pID, string pName, string pPrice, XmlTextWriter writer) { writer.WriteStartElement("Product"); writer.WriteStartElement("Product_id"); writer.WriteString(pID); writer.WriteEndElement(); writer.WriteStartElement("Product_name"); writer.WriteString(pName); writer.WriteEndElement(); writer.WriteStartElement("Product_price"); writer.WriteString(pPrice); writer.WriteEndElement(); writer.WriteEndElement(); } } } |
How to create XML file using C# – Output result
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<?xml version="1.0" encoding="utf-8" standalone="yes" ?> - <Table> - <Product> <Product_id>1</Product_id> <Product_name>Product 1</Product_name> <Product_price>1000</Product_price> </Product> - <Product> <Product_id>2</Product_id> <Product_name>Product 2</Product_name> <Product_price>2000</Product_price> </Product> - <Product> <Product_id>3</Product_id> <Product_name>Product 3</Product_name> <Product_price>3000</Product_price> </Product> - <Product> <Product_id>4</Product_id> <Product_name>Product 4</Product_name> <Product_price>4000</Product_price> </Product> </Table> |
If you like this topic, please provide feedback on comment box.
How to create XML file using C#






