Reading and Writing XML files

Source: Internet
Author: User
1. Write XML (create an XML file ):
Xmltextwriter mywriter = new xmltextwriter (file path, null );
// Document version 1.0 for writing XML
Mywriter. writestartdocument ();
// Write Start Element
Mywriter. writestartelement ("root ");
// Write Element
Mywriter. writestartelement ("");
Mywriter. writestring ("Tale ");
Mywriter. writeendelement ();

Mywriter. writestartelement ("B ");
Mywriter. writestring ("th ");
Mywriter. writeendelement ();

Mywriter. writestartelement ("C ");
Mywriter. writestring ("T3 ");
Mywriter. writeendelement ();

// Write the end document
Mywriter. writeenddocument ();
// Close the stream and save it in the file
Mywriter. Close ();


2. Read XML:

Xmlreader xmlr = xmlreader. Create (file path );
Xmlr. movetocontent ();
Xmlr. Read ();
If (xmlr. Name. tostring () = "databasename ")
{
Databasename = xmlr. readinnerxml (). tostring ();
// Read other elements in sequence
.........
}

In addition:
Xmlreader xmlr = xmlreader. Create ("ftp_set.xml"); // open the document to be read
Xmlr. movetocontent ();
While (xmlr. Read () // read cyclically
{
Switch (xmlr. Name. tostring ())
{
Case "Uri": // xml tag
This. strftpuri = xmlr. readinnerxml (). tostring ();
Break;
Case "potnumber ":
Strpotnum = xmlr. readinnerxml (). tostring ();
Break;
Case "username ":
This. strusername = xmlr. readinnerxml (). tostring ();
Break;
Case "password ":
This. strpassword = xmlr. readinnerxml (). tostring ();
Break;
}
}

3. Note: when reading XML, if the file contains blank spaces and press enter, a blank space and press enter will be read at a time. You can program to ignore these spaces and press Enter.

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.