Use xmltextwriter and xmltextreader to access and create and read XML files

Source: Internet
Author: User

 

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.