TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Xml Text Writer
Rajendra Tripathy
Aug 26, 2014
22.7
k
0
0
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
This blog describes, How Xml Text Writer works.
XmlTextWriter Represents a writer that provides a fast, non-cached, forward-only way of generating streams or files containing XML data. XmlTextWriter renders XML data to a string. This string remains in the memory of the C# program. XmlTextWriter is useful for in-memory generation of XML.
private
void
button1_Click(
object
sender, EventArgs e)
{
// Creates an instance of the System.Xml.XmlTextWriter class using the specified file.
XmlTextWriter textWriter =
new
XmlTextWriter(@
"C:\xmlfile.xml"
,
null
);
// Writes the XML declaration with the version "1.0".
textWriter.WriteStartDocument();
// Write comments
textWriter.WriteComment(
"First Comment for XmlTextWriter Sample Example"
);
// Write first element
textWriter.WriteStartElement(
"Student"
);
textWriter.WriteStartElement(
"Document"
,
"Test"
,
"RECORD"
);
// Write next element Name
textWriter.WriteStartElement(
"Name"
);
textWriter.WriteString(
"Rajendra"
);
textWriter.WriteEndElement();
// Write element Address
textWriter.WriteStartElement(
"Address"
);
textWriter.WriteString(
"Bangalore"
);
textWriter.WriteEndElement();
// Write element Pincode
textWriter.WriteStartElement(
"Pincode"
);
textWriter.WriteString(
"560066"
);
textWriter.WriteEndElement();
// WriteChars
char
[] ch =
new
char
[3];
ch[0] =
'R'
;
ch[1] =
'A'
;
ch[2] =
'J'
;
textWriter.WriteStartElement(
"Char"
);
textWriter.WriteChars(ch, 0, ch.Length);
textWriter.WriteEndElement();
// Ends the document.
textWriter.WriteEndDocument();
// close writer
textWriter.Close();
}
Result
Xml Text Writer
Next Recommended Reading
Binding XML File with GridView in .net