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 );