Reading And Writing XML Content Using C#

Reading and Writing XML content is important in any programming language. In this article, I will explain how an XML file can be written dynamically. I am going to describe two methods: one for showing xml content on to the console and another for writing xml contents to a .dat file.

.NET framework provides an XmlWriter class a static Create method that expects either a FileStream or stringwriter to write contents to a file and to show contents on the console respectively.

Once xmlwriter object is instantiated with stream object. It can further be populated with relevant information.

Let’s say we’ve the following XML structure to be written:

Ex:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2.   <Person>  
  3.      <Employee>  
  4.         <Name>John</Name>  
  5.         <Address>London </Address>  
  6.       </Employee>  
  7.   </Person>  
Standard methods are listed below to accomplish the task stated above:

xmlWriter.WriteStartElement: Writes out a start tag with the specified local name.
xmlWriter.WriteElementString: Writes an element containing a string value.
xmlWriter.WriteEndDocument: Closes any open elements or attributes and puts the writer back in the Start state.
  1. private static void Main(string[] args)   
  2. {  
  3.     //Show XML on to Console  
  4.     ShowXMLOnConsole();  
  5.     //Write XML to a File  
  6.     WriteDyanmicallyCreatedXmlToFile();  
  7.     Console.ReadKey();  
  8. }  
  9.   
  10. /// <summary>  
  11. /// Creates a xml file and  
  12. /// Writes xml content dynamically  
  13. /// </summary>  
  14. private static void WriteDyanmicallyCreatedXmlToFile()                                   
  15.  {  
  16.     FileStream writer = new FileStream(@"c:\temp\xmlcontent.dat", FileMode.CreateNew);  
  17.   
  18.     using(XmlWriter xmlWriter = XmlWriter.Create(writer))  
  19.      {  
  20.         xmlWriter.WriteStartElement("Person");  
  21.         xmlWriter.WriteStartElement("Employee");  
  22.         xmlWriter.WriteElementString("Name""John");  
  23.         xmlWriter.WriteElementString("Address""London");  
  24.   
  25.         xmlWriter.WriteEndDocument();  
  26.   
  27.         writer.Flush();  
  28.     }  
  29.   
  30.     string xmlContent = writer.ToString();  
  31.   
  32.     Console.WriteLine(xmlContent);  
  33. }  
  34.   
  35. private static void ShowXMLOnConsole()  
  36.  {  
  37.     StringWriter writer = new StringWriter();  
  38.     using(XmlWriter xmlWriter = XmlWriter.Create(writer))   
  39.     {  
  40.         xmlWriter.WriteStartElement("Person");  
  41.         xmlWriter.WriteStartElement("Employee");  
  42.         xmlWriter.WriteElementString("Name""John");  
  43.         xmlWriter.WriteElementString("Address""London");  
  44.   
  45.         xmlWriter.WriteEndDocument();  
  46.   
  47.         writer.Flush();  
  48.     }  
  49.   
  50.     string xmlContent = writer.ToString();  
  51.   
  52.     Console.WriteLine(xmlContent);  
  53. }  
For more information on methods available, refer to Microsoft documentation:

 

Up Next
    Ebook Download
    View all
    Learn
    View all