Accessing and Manipulating XML Data in Microsoft .NET Framework
by Team uCertifyuCertify.com
Wednesday, 23rd August 2006
The XmlWriter Class
The XmlWriter class is an abstract class of the System.Xml namespace that creates streams and writes data to XML documents. The XmlWriter class performs tasks such as writing various documents into one output stream, encoding binary data, and managing, flushing and closing the output stream. The XmlTextWriter class derived from the XmlWriter class provides properties and methods for writing XML data to an output file, stream or console. The following are some of the properties and methods of the XmlTextWriter class:
- BaseStream Property Returns the stream to which the XmlTextWriter is writing.
- Formatting Property Specifies the formatting of the output in either Indented or None form.
- WriteState Property Specifies the state value of the writer that includes Start, Element, Attribute, Content, Prolog, and Closed.
- WriteStartDocument() Method Writes the XML declaration <?xml version = "1.0" ?> to the start of the XML document.
- WriteStartElement() Method Specifies the start tag of a specified element.
- WriteEndElement() Method Specifies the end tag of a specified element.
- WriteStartAttribute() Method Specifies the start of an attribute.
- WriteEndAttribute() Method Specifies the end of an attribute.
The following code snippet written in Visual Basic .NET shows how to write data using the XmlTextWriter class:
Dim writedata As New XmlTextWriter("e:\students.xml", System.Text.Encoding.UTF-8)
writedata.Formatting = Formatting.Indented
writedata.WriteStartDocument(False)
writedata.WriteDocType("StudentRecords", Nothing, Nothing, Nothing)
writedata.WriteComment("This file represents a fragment of Employees" & "database")
writedata.WriteStartElement("StudentRecords")writedata.WriteStartElement("Students", Nothing)writedata.WriteElementString("StudentID", "SO100" )writedata.WriteElementString("StudentName", "John Trivolta")writedata.WriteElementString("StudentSubject", "Science")writedata.WriteEndElement()writedata.WriteEndElement()[
// Write the XML to file and close the text
Writerwritedata.Flush()
textWriter.Close()
Console.WriteLine("Press <Enter> to exit")Console.Read()
writedata.Formatting = Formatting.Indented
writedata.WriteStartDocument(False)
writedata.WriteDocType("StudentRecords", Nothing, Nothing, Nothing)
writedata.WriteComment("This file represents a fragment of Employees" & "database")
writedata.WriteStartElement("StudentRecords")writedata.WriteStartElement("Students", Nothing)writedata.WriteElementString("StudentID", "SO100" )writedata.WriteElementString("StudentName", "John Trivolta")writedata.WriteElementString("StudentSubject", "Science")writedata.WriteEndElement()writedata.WriteEndElement()[
// Write the XML to file and close the text
Writerwritedata.Flush()
textWriter.Close()
Console.WriteLine("Press <Enter> to exit")Console.Read()
The following code snippet written in Visual C# .NET shows how to write data using the XmlTextWriter class:
XmlTextWriter writedata = new XmlTextWriter("e:\students.xml", System.Text.Encoding.UTF-8);
writedata.Formatting = Formatting.Indented;
writedata.WriteStartDocument(false);
writedata.WriteDocType("StudentRecords", null, null, null);
writedata.WriteComment("This file represents a fragment of Employees" + "database");
writedata.WriteStartElement("StudentRecords");
writedata.WriteStartElement("Students", null);
writedata.WriteElementString("StudentID", "SO100" );
writedata.WriteElementString("StudentName", "John Trivolta");
writedata.WriteElementString("StudentSubject", "Science");
writedata.WriteEndElement();
writedata.WriteEndElement();
// Write the XML to file and close the text
Writerwritedata.Flush();
writedata.Close();
Console.WriteLine("Press <Enter> to exit");
Console.Read();
writedata.Formatting = Formatting.Indented;
writedata.WriteStartDocument(false);
writedata.WriteDocType("StudentRecords", null, null, null);
writedata.WriteComment("This file represents a fragment of Employees" + "database");
writedata.WriteStartElement("StudentRecords");
writedata.WriteStartElement("Students", null);
writedata.WriteElementString("StudentID", "SO100" );
writedata.WriteElementString("StudentName", "John Trivolta");
writedata.WriteElementString("StudentSubject", "Science");
writedata.WriteEndElement();
writedata.WriteEndElement();
// Write the XML to file and close the text
Writerwritedata.Flush();
writedata.Close();
Console.WriteLine("Press <Enter> to exit");
Console.Read();
Options:
Printer Friendly
Email Friend
About The Author:
uCertify was formed in 1996 with an aim to offer high quality educational training software and services in the field of information technology to its customers. uCertify provides exam preparation solutions for the certification exams of Microsoft, CIW, CompTIA, Oracle, Sun and other leading IT vendors. To know more about uCertify, please visit http://www.ucertify.com/
uCertify was formed in 1996 with an aim to offer high quality educational training software and services in the field of information technology to its customers. uCertify provides exam preparation solutions for the certification exams of Microsoft, CIW, CompTIA, Oracle, Sun and other leading IT vendors. To know more about uCertify, please visit http://www.ucertify.com/
