Common XML operations in. net

Source: Internet
Author: User
1 Introduction to System Classes 1.1 xmldocument (DOM)

It is easy to read XML into the memory and is not suitable for reading large text.

Namespace: system. xml

1.2 xmlreader (SAX)

Without loading text to the memory, you can directly read the text. The memory usage is low and you can quickly read specified nodes in the file.

Namespace: system. xml

1.3 XMLSCHEMA

XML architecture, used to verify the validity of XML text.

Namespace: system. xml. Schema

2 typical application example 2.1 xmldocument // create an xmldocument object

Xmldocument oxmldocument = new xmldocument ();

// Oxmldocument. Load (filename); // read the entire file

 

// Add an XML file Declaration

Xmldeclaration = oxmldocument. createxmldeclaration (xmlversion, xmlencoding, null );

Oxmldocument. appendchild (xmldeclaration );

 

// Add the power_exchange Root Node

Xmlelement root = oxmldocument. createelement ("power_exchange ");

Root. setattribute ("xmlns: xsi", "http://www.w3.org/2001/XMLSchema-instance ");

Root. setattribute ("xsi: nonamespaceschemalocation", "Exchange. XSD ");

Oxmldocument. appendchild (Root );

2.2 xmlreader

// Read from a file
Xmlreader READ = xmlreader. Create (filename );

Read. readtofollowing ("object ");

String sname = xread. getattribute ("name ");

String sverb = xread. getattribute ("verb ");


// Read from an XML string
Stringreader sreader = new stringreader (sxml );

Xmlreader = xmlreader. Create (sreader );

Read. readtofollowing ("object ");

String sname = xread. getattribute ("name ");

String sverb = xread. getattribute ("verb ");
2.3 XMLSCHEMA // create a schema validating xmlreader.

Xmlreadersettings settings = new xmlreadersettings ();

Settings. schemas. Add (targetnamespace, xsdfile );

Settings. validationeventhandler + = new validationeventhandler (validationeventhandler );

Settings. validationflags = settings. validationflags | xmlschemavalidationflags. reportvalidationwarnings;

Settings. validationtype = validationtype. Schema;

 

Xmlreader reader = xmlreader. Create (xmlfile, settings );

 

// The xmldocument validates the XML document contained

// In the xmlreader as it is loaded into the Dom.

Xmldocument document = new xmldocument ();

Document. Load (Reader );

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.