Code
Using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. text;
Using system. Windows. forms;
Using system. xml;
Namespace xmlsaxdemo
{
Public partial class form1: Form
{
Public form1 ()
{
Initializecomponent ();
}
Private void btnwrite_click (Object sender, eventargs E)
{
String filename = "books. xml ";
Xmltextwriter writer = new xmltextwriter (filename, null );
Writer. Formatting = formatting. indented;
Writer. indentation = 6;
Writer. writestartdocument ();
Writer. writestartelement ("books ");
Writer. writestartelement ("book ");
Writer. writeelementstring ("title", "window form ");
Writer. writeelementstring ("ISBN", "111111 ");
Writer. writeelementstring ("author", "amandag ");
Writer. writestartelement ("price ");
Writer. writeattributestring ("unit", "$ ");
Writer. writevalue ("128.00 ");
Writer. writeendelement ();
Writer. writeendelement ();
Writer. writestartelement ("book ");
Writer. writeelementstring ("title", "ASP. NET ");
Writer. writeelementstring ("ISBN", "222222 ");
Writer. writeelementstring ("author", "moon ");
Writer. writestartelement ("price ");
Writer. writeattributestring ("unit", "___ fckpd ___ 0 quot ;);
Writer. writevalue ("111.00 ");
Writer. writeendelement ();
Writer. writeendelement ();
Writer. writeendelement ();
Writer. writeenddocument ();
Writer. Close ();
}
Private void btnread_click (Object sender, eventargs E)
{
String filename = "books. xml ";
Xmltextreader reader = new xmltextreader (filename );
String STR = string. empty;
While (reader. Read ())
{
If (reader. nodetype = xmlnodetype. element)
{
If (reader. localname. Equals ("title") | reader. localname. Equals ("author "))
{
STR + = reader. readstring () + "";
}
}
}
MessageBox. Show (STR );
}
}
}
Http://blog.csdn.net/amandag/archive/2008/07/08/2623320.aspx