Wednesday, July 1, 2009

HOW TO WRITE XML IN C#

Below is the code in C# which you can use to create an XML file.



Note: You must include System.XML namespace. You can do so by writing the below command.

using System.XML;

The sample of XML is shown below:

<?xml version="1.0"?>
<Library>
<Subjects name="Computer Science">
<Publishers name="Wrox">
<Begginers>Beginning C#</Begginers>
</Publishers>
</Subjects>
</Library>

Now write the below code to get the above result.

XmlTextWriter writer = new XmlTextWriter(filename, null);
//SELECT THE FORMATTING STYLE (INDENTED, NONE)
writer.Formatting = Formatting.Indented;
//STARTING TAG
writer.WriteStartDocument();
//STARTING "Root"
writer.WriteStartElement("Library");
//STARTING ELEMENT NAMED "COMPUTER SCIENCE"
writer.WriteStartElement("Subjects");
writer.WriteAttributeString("name", "Computer Science");
//FURTHER ELEMENT NAMED "COMPUTER SCIENCE"
writer.WriteStartElement("Publishers");
writer.WriteAttributeString("name", "Wrox");
writer.WriteStartElement("Begginers");
writer.WriteString("Beginning C#");
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteEndDocument();
writer.Close();
Console.WriteLine("XML Written Successfully");

No comments:

Docker Tutorial

 I have listed all the necessary commands of Docker below. In case if any is missing or if any improvement required, please share in comment...