Write a XML string using XmlWriter

Madhu.b.rokkam
Posted by Madhu.b.rokkam under C# category on | Points: 40 | Views : 4446
static void Main(string[] args)
{
StringBuilder sb = new StringBuilder();
using (StringWriter stringWriter = new StringWriter(sb))
using (XmlTextWriter xtw = new XmlTextWriter(stringWriter))
{
xtw.Formatting = Formatting.Indented;
xtw.WriteStartElement("School");
xtw.WriteAttributeString("Registered", "2009");
xtw.WriteStartElement("Name");
xtw.WriteString("Matrusri Anglo Vedic School");
xtw.WriteEndElement();
xtw.WriteStartElement("City");
xtw.WriteString("Berhampur");
xtw.WriteEndElement();
xtw.WriteEndElement();
}
Console.WriteLine(sb.ToString());
Console.Read();
}


Output

<School Registered="2009">
<Name>Matrusri Anglo Vedic School</Name>
<City>Berhampur</City>
</School>

Comments or Responses

Login to post response