TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to C-SHARP

C# XmlTextWriter

XmlTextWriter renders XML data to a string. This string remains in the memory of the C# program. We use an underlying buffer—in this case, a StringWriter instance. XmlTextWriter is useful for in-memory generation of XML.XmlTextReader
First, this program includes the System.Xml and System.IO namespaces. These are for the XmlTextWriter and also the StringWriter. The program creates an array of four Tuple instances. These represent employee data in a small company.StringWriter

Info: The StringWriter is used as a backing store for the XmlTextWriter. XmlTextWriter to the StringWriter instance you passed to it.

Next: We see the WriteStartDocument and WriteStateElement methods. These simply begin the XML document and then begin an element.

Note: When you pass a string to WriteStartElement, the XmlTextWriter then will know the element tag you are writing.

Foreach: Next, we see a foreach loop that enumerates the array of Tuple instances. In each iteration of the loop, we write an Employee element.

Foreach
C# program that uses XmlTextWriter using System; using System.IO; using System.Xml; class Program { static void Main() { // Create an array of four Tuples. var array = new Tuple<int, string, string, int>[4]; array[0] = new Tuple<int, string, string, int>(1, "David", "Smith", 10000); array[1] = new Tuple<int, string, string, int>(3, "Mark", "Drinkwater", 30000); array[2] = new Tuple<int, string, string, int>(4, "Norah", "Miller", 20000); array[3] = new Tuple<int, string, string, int>(12, "Cecil", "Walker", 120000); // Use StringWriter as backing for XmlTextWriter. using (StringWriter str = new StringWriter()) using (XmlTextWriter xml = new XmlTextWriter(str)) { // Root. xml.WriteStartDocument(); xml.WriteStartElement("List"); xml.WriteWhitespace("\n"); // Loop over Tuples. foreach (var element in array) { // Write Employee data. xml.WriteStartElement("Employee"); xml.WriteElementString("ID", element.Item1.ToString()); xml.WriteElementString("First", element.Item2); xml.WriteWhitespace("\n "); xml.WriteElementString("Last", element.Item3); xml.WriteElementString("Salary", element.Item4.ToString()); xml.WriteEndElement(); xml.WriteWhitespace("\n"); } // End. xml.WriteEndElement(); xml.WriteEndDocument(); // Result is a string. string result = str.ToString(); Console.WriteLine("Length: {0}", result.Length); Console.WriteLine("Result: {0}", result); } } } Output Length: 441 Result: <?xml version="1.0" encoding="utf-16"?><List> <Employee><ID>1</ID><First>David</First> <Last>Smith</Last><Salary>10000</Salary></Employee> <Employee><ID>3</ID><First>Mark</First> <Last>Drinkwater</Last><Salary>30000</Salary></Employee> <Employee><ID>4</ID><First>Norah</First> <Last>Miller</Last><Salary>20000</Salary></Employee> <Employee><ID>12</ID><First>Cecil</First> <Last>Walker</Last><Salary>120000</Salary></Employee> </List>
WriteWhitespace. You will notice there are some calls to WriteWhitespace in the code. These are present to make the output look a bit nicer. They do not affect the correctness of the XML.

Finally: We see a matching pair of WriteEnd methods. These match the WriteStartDocument and WriteEndElement methods called earlier.

Tip: It is important you match all Start methods with End methods. The program will not work correctly unless you do this.

Summary. This tutorial exposes several important principles of using XmlTextWriter. After constructing XmlTextWriter with a backing stream, you must use WriteStartDocument. Then WriteStartElement and WriteElementString can construct elements.

Finally: All Start methods must be matched by End methods such as WriteEndDocument. We returned the XML result in a string instance.

© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf