. NET is used to process XML methods and related classes

zhaozj2021-02-16  43

This article is some of the ".NET XML Advanced Programming", I hope to use primary readers who want to make .NET XML programming.

For those who use .NET for XML programming, I don't know if you feel like this: .NET although there is a lot of methods for processing XML, such as: XmlReader, XMLWriter, XMLDocument, XpathDocument, Xpathnavigator Xsltransfrom and DomDocument as a COM component. But every time I truly XML programming, I always feel that there is no way, and these classes are doing, what is it necessary to use it or how it is possible? So although the powerful function of the processing XML provided for .NET can only be expected. Ok, there is this article, you will have a clear understanding and understanding of XML in .NET, I think this is enough to be enough to you.

In general, the method of processing XML in .NET can be divided into two categories: XML hosted classes and COM components:

1, use XML hosted classes

The XML custodian class is divided into two types of stream-based APIs based on .NET, all types of APIs based on traditional DOM programming models are located in the System.xml program. The assembly contains objects that directly process the XML document (which includes only the forward non-cached access objects (similar to SAX), including random access objects (similar to document object model DOM) that loads the entire document to memory. And include some assistant classes (such as name forms such as XML analyzer performance for improving MS).

A. Brand new-based API (XMLReader and XMLWRITER) is implemented by .NET

They are using a stream type I / O model (only a non-buffered access model). XmlReader is used to process XML documents, while XMLWRITER is used to generate an XML document (serialization). They become an excellent method for replacing traditional DOM methods (see B), because the flow model does not require a high cost memory cache.

XmlReader provides a method that can be used to navigate in a document (mainly a method of access to a node and some assistant methods for quick navigation), and also provides a method of reading the current node information.

// Example Program: Read an XML stream from an XML file using the XMLReader class

Using system;

USING SYSTEM.XML;

Class class1

{

Static void main (string [] args)

{

XmlTextReader Reader = NULL;

Int iCount = 0;

Try

{

Reader = New XMLTextReader (@ "C: /1.xml");

While (Reader.Read ())

{

IF (reader.NodeType == XMLNodetype.element)

ICOUNT ;

}

Console.write ("Total {0} Elements Found!", ICOUNT);

}

Finally

{

IF (reader! = null)

Reader.Close ();

}

}

}

// Sample Program: Use the XMLWRITER class to create a format correct XML document one by one node

Using system;

USING SYSTEM.XML;

Class class1

{

Static void main (string [] args)

{

XmlTextWriter Writer = new xmlTextWriter (console.out); Writer.Formatting = formatting.indented;

Writer.writestartDocument ();

Writer.writestartElement ("Person");

Writer.writeAttributeString ("SEX", "Male");

Writer.writeAttributeString ("firstname", "sunny");

Writer.writeAttributeString ("Lastname", "Bajaj");

Writer.writeElementstring ("Phone", "111-111-1111");

Writer.WriteElementstring ("Phone", "222-222-2222");

Writer.writeEndelement ();

Writer.writeEndDocument ();

Writer.flush ();

Writer.close ();

}

}

B, API (DOM, XPath, XSLT) based on traditional DOM programming model

I, DOM

With the DOM API, the entire XML document will be loaded into memory (cache), the API allows navigation and editing of the memory XML document, you can use the DOM to create an XML document, then save it to a file, or send it to one Stream medium. In .NET, the DOM's load mechanism is built on the XMLReader class, while DOM serialization is built on the XMLWRITER class. The core class is XMLDocument (which is equivalent to MSXML DomDocument accompanying class in COM components, the usage of both is also very similar), it is an important way to load () and loadXML () methods.

Ii, XPath

In .NET, XPath namespace supports document navigation. The XPath function is implemented by the XPathnavigator class.

// Example Program: Demonstrate Usage of XPath

// c: /1.xml

111-111-1111

222-222-2222

Using system;

Using system.xml.xpath;

Class class1

{

Static void main (string [] args)

{

XpathDocument doc = new xpathdocument (@ "c: /1.xml");

Xpathnavigator NAV = doc.createnavigator ();

XpathnodeEiterator it = nav.select ("/ person / phone");

While (item.movenext ())

{

Console.Writeline ("Phone: {0}", iter.current.value);

}

}

}

III, XSLT conversion is used to convert XML from a format to another. The core class is XslTransform, and the important way for this class is load () and transform ().

// Example program: Convert C: /1.xml to another XML and output

// c: /1.xsl file

// xsltransformexample.cs

Using system;

USING SYSTEM.XML;

Using system.xml.xpath;

Using system.xml.xsl;

Class class1

{

Static void main (string [] args)

{

// load input document

XmlDocument doc = new xmldatadocument ();

Doc.Load (@ "c: /1.xml");

Xpathnavigator NAV = doc.createnavigator ();

// load xslt document

Xsltransform Docxsl = new xsltransform ();

Docxsl.Load (@ "c: /1.xsl");

// Apply Transformation and Send Output to Console

Docxsl.transform (NAV, NULL, Console.out);

}

}

If you are writing an ASP.NET web application, you need to display an XML document or you need to use the server-side XML style sheet (ie .xsl file) to convert an XML document, this may need to use XML Web Server Controls

Second, use COM components (COM interoperability)

When processing XML in .NET, you should try to use the XML class in the System.xml program, instead of using MSXML through the interoperability of COM.

Assuming the latest version of MSXML provides a function that the .NET XML hosted class (ie, the System.xml assembly) does not support. At this time, we must use MSXML through the COM interoperability mechanism in the .NET application. . It is located in Microsoft XML Parser MSXML 3.0 in the COM tab.

// Example Program: Using MSXMlusing System;

USING MSXML2;

Class class1

{

Static void main (string [] args)

{

Domdocument30 doc = new domdocument30 ();

Doc.Load (@ "c: /books.xml");

Console.Write (Doc.xml);

}

}

Ok, because of the time relationship, write here first. In the future, I will launch a detailed version of this article at appropriate, I hope you will pay attention.

转载请注明原文地址:https://www.9cbs.com/read-26534.html

New Post(0)