307494 HOW TO: Use Visual C # .NET to apply XSL conversions to XML for streaming (from mkba)

xiaoxiao2021-03-06  73

The release number of this article has been CHS307494

For Microsoft Visual Basic .NET versions of this article, see

300934.

This task content

Summary requires XSL to XML to the description of the description of stream processing to generate an example

Summary This article describes how to use it

The XSLTransform class applies the Extensible Style Sheet Language (XSL) Conversion (XSLT) language to the Scalable Markup Language (XML) document to create a new XML document. XSL is an XML-based language that is intended to convert an XML document to another XML document or convert an XML document to any other structured document.

Back to top

Require the following list lists the recommended hardware, software, network structure, and service packs required:

Microsoft Visual Studio .NET Microsoft Visual Studio .NET Software Development Kit (SDK) Quick Start This article assumes that you are familiar with the following topics:

XML technology creates and read XML file XML Path Language (XPath) Syntax XSL

Back to top

How to apply the XSL conversion This example uses two files named books.xml and books.xsl. You can use your own books.xml and books.xsl files or use the sample file included in the Quick Gateway using the .NET Software Development Kit (SDK). Books.xml and books.xsl files must be copied to the / bin / debug folder, which is below the folder where the project is created. These files can be found in the following folders:

../Program files / microsoft visual studio .NET / FRAMEWORKSDK / SAMPLES / QuickStart / HOWTO / SAMPLES / XML / Transformxml / CS

Create a console application in Visual C # .NET. Make sure the project contains a reference to System.xml namespaces; if not included, add this reference. Use the USING statement for XML, XPath and XSL namespaces, so that there is no need to define declarations in these namespaces in the following code. The USING statement must be before any other declaration. USING SYSTEM.XML;

Using system.xml.xsl;

Using system.xml.xpath; declare the corresponding variable. Declare an XPathDocument object to save an XML document, declare an XSLTransform object to convert an XML document. Add a declaration code during the MAIN of MAIN: XslTransform myxsltransform;

XpathDocument MyXPathDocument; populate the XPathDocument object with the Books.xml sample file. The XPathDocument class provides a fast and performance-oriented cache to use XSLT to process an XML document. The XPathDocument class is similar to the XML document object model (DOM), but the former is highly optimized for the XSLT processing and the XPath data model. MyXPathDocument = New XPathDocument ("Books.xml"); build a new XslTransform object. XSLTransform class is an XSLT processor for implementing XSL conversion (XSLT) 1.0 recommended code: MyxSLTransform = New xsltransform (); loads the xsltransform object with this style table using the LOAD method. This style sheet converts the details of the books.xsl document to a concise international standard book number (ISBN) list of books: MyXSLTransform.Load ("Books.xsl"); Create an XMLTextWriter class with the new XML file name after the conversion. Call the Transform method to start the conversion. XmlTextWriter Writer = New XmlTextWriter ("isbnbooks.xml", system.text.encoding.utf8); MyxSltransform.Transform (MyXPathDocument, Null, Writer);

Writer.flush ();

Writer.close (); or you can also send the converted XML document to the XMLReader, Stream, or Textwriter class. The following code example sends an XML transition to the instance of StringWriter (derived by TextWriter), and this instance is rotated to write the conversion to the console window. System.io.stringwriter stwrite = new system.io.stringWriter ();

MyXSLTransform.Transform (MyXPathDocument, null, stwrite);

Console.writeline (stwrite.tostring);

Console.readLine (); Note: The full code list uses the above code without using the code in step 8. Generate and run your project. The result of the conversion is displayed in the console window, as shown below: 11.99

9.99

Back to top

Complete code example

Using system;

USING SYSTEM.XML;

Using system.xml.xsl;

Using system.xml.xpath;

Using system.io;

Namespace XSLTransformFromxPath

{

///

/// summary description for class1.

///

Class class1

{

Static void main (string [] args)

{

Xsltransform myxsltransform;

XpathDocument MyXPathDocument = New XPathDocument ("Books.xml");

Myxsltransform = new xsltransform ();

MYXSLTRANSFORM.LOAD ("Books.xsl");

XmlTextWriter Writer = New XMLTextWriter ("ISBNBOOKS.XML", System.Text.Encoding.utf8);

MyXSLTransform.Transform (MyXPathDocument, Null, Writer);

Writer.flush ();

Writer.close ();

System.io.stringwriter stwrite = new system.io.stringWriter ();

MyXSLTransform.Transform (MyXPathDocument, null, stwrite);

Console.writeline (stwrite.tostring ());

Console.readline ();

}

}

}

Back to top

Reference

For more information on the XMLReader class, see the following Microsoft .NET Framework Class library documentation:

http://msdn.microsoft.com/fibrary/dotnet/cpref/frlfsystemxmlxslxsltransformClasstopic.htm About included

Xsltransform object

For information on the XSLTransform class, see the following Microsoft .NET Framework Development Guide document:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguidnf/html/cpconxsltransformclassimplementsxsltprocessor.asp?frame=true practical comparison of XSLT and Active Server Pages .NET, see The following MSDN Article:

Http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnexxml/html/xml02192001.asp For more information on .NET, please review the following Microsoft Web Site

".NET Framework XML Classes and C # Offer Simple, Scalable Data Manipulation" (. NET Framework XML class and C # provide simple scalable data operations)

http://msdn.microsoft.com/msdnmag/issues/01/01/xml/xml.asp

Back to top

The information in this article applies to:

Microsoft Visual C # .NET (2002)

Recent Updated: 2002-2-24 (1.0) Keyword Kbhowto KbhowTomaster KB307494

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

New Post(0)