XML plays an important role in. NET Framework. For example, XML is used in configuration files and source code documents of. NET Framwork, and XML is also used for SOAP, WEB Services, and ADO. NET.
To expand XML,. NET Framwork contains the System. Xml namespace. This namespace contains many classes that process XML. For example, XmlDocument (this is the DOM Implementation Method) class, And. NET provides a substitute for SAX (XmlReader and XmlWriter class ).
XPath and XSLT classes. The class in the namespace of System. Xml. Serialization creates an object (deserialization) from the XML document ).
The following table lists the classes for reading and writing XML data.
There are two methods to operate XML
1. Use MSXML
MSXML is an XML analyzer and MSXML is a COM-based component. Therefore, you need to create an assembly for interactive operations. The simplest way is to add reference to this COM component (Microsoft XML, v4.0 (or v3.0, v2.6) in. MSXML2 will appear in the reference column (when the COM component is imported, the namespace provided for the new assembly is the type library name of the COM component ).
Ii. Use the System. Xml class
Compared with the msxml class, the System. Xml class has several advantages. First, System. Xml is a managed code that ensures that all code is secured and type-safe. Using COM interactive operations will increase some overhead, but the most important thing is that the System. Xml namespace is easy to use and flexible.
Read/write stream format XML
XmlReader provides a very fast read-only cursor to process XML data because the memory requirement is not very high.
XmlWriter can generate XML documents.
Both XmlReader and XmlWriter are abstract classes.
Classes derived from XmlReader include XmlNodeReader (using XmlNode as its source rather than a stream) and XmlValidatingReader (XmlValidatingReader adds DTD and mode verification to provide data validity verification), XmlTextReader (used with the TextReader object in the IO namespace)
XmlTextWriter (used together with the TextWriter object in the IO namespace)
1. XmlReader class
XmlReader is very similar to SAX. The biggest difference between them is that SAX is a push model (all XML data must be processed by the application, whether or not the data is required ), xmlReader is a pull model (you do not need to process all the data if you do not need it ).
The following code:
richTextBox1.Clear(); XmlReader rdr = XmlReader.Create("book.xml"); while (rdr.Read()) { if (rdr.NodeType == XmlNodeType.Text) richTextBox1.AppendText(rdr.Value+"\r\n"); } |
(1) Use the static method Create () to return an XmlReader object.
(2) The Read () method can enter the next node.
The XmlReader class can also read strongly typed data. It has several ReadValuesAs methods, such as ReadValueAsDouble and ReadValueAsBoolean.
Retrieving attribute data
The AttributeCountry attribute determines the number of attributes. The GetAttribute () method obtains attributes by name or index. To iterate an attribute at a time, you can use the MoveToFirstAttribute () and MoveToNextAttribute () methods.
The following code:
richTextBox1.Clear(); XmlReader tr = XmlReader.Create("book.xml"); while (tr.Read()) { if (tr.NodeType == XmlNodeType.Element) { for (int i = 0; i < tr.AttributeCount; i++) { richTextBox1.AppendText(tr.GetAttribute(i)+"\r\n"); } } } |
Use the XmlReader class for verification
Sometimes we need to know whether the document format is standard or whether the document is valid.
XmlReader can use XmlReaderSettings to verify XML based on the XSD mode. Add the XSD mode to XMLSchemaSet and access XMLSchemaSet through the Schema attribute. The XsdValidate attribute must also be set to true, which is flase by default.
The XmlWriter class can write Xml into a stream, file, StringBuilder, TextWriter, or another XmlWriter object. Like XmlReader, The XmlWriter class writes data only forward and Uncached.
Use XmlWirterSettings to configure whether to indent text and indentation.
The following code:
XmlWriterSettings settings = new XmlWriterSettings (); Settings. Indent = true; // whether to Indent Settings. NewLineOnAttributes = true; // write each attribute in one row, which makes it easier to read XML XmlWriter writer = XmlWriter. Create ("booknew. xml", settings ); Writer. WriteStartDocument (); Writer. WriteStartElement ("book "); Writer. WriteAttributeString ("genre", "Mystery "); WriteAttributeString ("publicationdate", "2001 "); Writer. WriteAttributeString ("ISBN", "123456489 "); Writer. WriteElementString ("title", "Case of the Money "); Writer. WriteStartElement ("author "); Writer. WriteElementString ("name", "Cookie Monster "); Writer. WriteEndElement (); Writer. WriteElementString ("price", "9.99 "); Writer. WriteEndDocument (); Writer. Flush (); Writer. Close (); |
1. Use the XmlWriterSettings instance object to set the generated XML.
2. Use Create () to return an XmlWriter object, where Create (), the first parameter is the Xml name, and the second parameter is the XmlWriterSettings instance object.
3. Use the Document declaration in WriterStartDocument () to start Data Writing and end with WriteEndDocument. Controls the nesting of elements. Note the call and position of WriterStartElement () and WriterEndElement.
4. There are also some dedicated writing methods. WriteCData () can output a CData section ( ), WriterComment () write comments in the correct XML format. WriteChae () writes the content of the character buffer.