301282 HOW TO: Write XML (from MKBA) to the file

xiaoxiao2021-03-06  77

The release number of this article has been CHS301282

This task content

summary

Require to write XML reference to the file

Summary This article describes how to use

The XMLTextWriter class writes XML in the file.

XMLTextWriter provides a quick way to generate XML, which can help you create an XML document.

The XMLTextWriter is based on streaming instead of using an object model such as an XML Document Object Model (DOM), therefore has better performance.

Back to top

Requirements the following software, software, network architecture, and service packs required for recommendations: SERVICE PACK:

Microsoft Windows 2000 Professional, Windows 2000 Server, Windows 2000 Advanced Server or Windows NT 4.0 Server Microsoft Visual Studio .NET This article assumes that you are familiar with the following topics:

XML terminology creates and read XML files

Back to top

Write XML to the file

Open Visual Studio .NET. Create a new Microsoft Visual Basic or Microsoft C # console application. If you want to view the complete code, you can jump directly to step 12. Here is a more detailed steps. Make sure the project references the System.xml namespace. Use the Imports statement for system.xml namespace, so that you don't need to limit XMLTextReader declaration in your code. The imports statement must be placed before all other declarations: Visual Basic Imports System.xmlc # using system.xml; constructs an XMLTextWriter. Typically, if you want to write XML as a "original" data type without DOM overhead, you should use XMLTextWriter, which provides a faster writing mechanism for writing XML. XMLTextWriter has a different constructor to specify where to write XML data. Next, you will write XML to Newbooks.xml files. Adding code to the main structure XmlTextWriter process in the main module: Visual Basic Dim myXmlTextWriter As XmlTextWriter = new XmlTextWriter ( "newbooks.xml", System.Text.Encoding.UTF8) C # XmlTextWriter myXmlTextWriter = new XmlTextWriter ( "newbooks.xml ", System.text.encoding.utf8); this constructor will get the file name of the file you want to write and the encoding you wish to generate. Use the formatting property to specify how to set the XML setting as a format. In this way, the child element can be indented by using Indentation and IndentChar properties. Visual Basic MyXMLTextWriter.Formatting = system.xml.formatting.indentedC # myXmlTextWriter.Formatting = formatting.indented; Write XML files from the XML declaration by using the WriteStartDocument method. Visual Basic MyXMLTextWriter.WriteStartDocument (false) C # myXmlTextWriter.writestartDocument (false); write annotations using WriteComment methods as needed. Visual Basic myXmlTextWriter.WriteComment ( "This is a comment") C # myXmlTextWriter.WriteComment ( "This is a comment"); create XML element nodes and text nodes WriteStartElement, WriteEndElement, WriteString and WriteElementString method. The WriteElementString method will write a string (if any) provided by the parameter (if any) provided as a parameter and ending this element within one line. Visual Basic 'Create The Main Document Element.myxmlTextWriter.writestartElement ("BookStore")

MyXMLTextWriter.writestartElement ("Book")

'Create An Element Named' Title 'with a Text Node.' And The Close The Element

MyXMLTextWriter.writestartElement ("Title")

MyXmlTextWriter.writestring ("The Autobiography of Mark Twain)

MyXmlTextWriter.writeEndelement ()

'Create an element named' author '.

MyXmlTextWriter.writestartElement ("author")

'Create An Element Named' First-Name 'with a TEXT NODE

'and close it in one line.

MyXMLTextWriter.writeElementString ("First-Name", "Mark")

'Create an element named' first-name 'with a text node.

MyXmlTextWriter.writeElementString ("Last-Name", "TWAIN")

'Close Off The Parent Element.

MyXmlTextWriter.writeEndelement ()

'Create An Element Named' Price 'with a Text Node

'and close it in one line.

MyXMLTextWriter.writeElementstring ("Price", "7.99")

'Close Off All Elements.

MyXmlTextWriter.writeEndelement ()

MyXmlTextWriter.writeEndelement ()

C # myXmlTextWriter.writestartElement ("BookStore");

MyXMLTextWriter.writestartElement ("BOOK", NULL);

MyXMLTextWriter.writeElementsTRING ("Title", NULL, "The Autobiography of Mark TWAIN);

MyXMLTextWriter.writestartElement ("Author", null;

MyXMLTextWriter.writeElementString ("first-name", "mark");

MyXMLTextWriter.writeElementsTRING ("Last-Name", "TWAIN");

MyXmlTextWriter.writeEndelement ();

MyXMLTextWriter.writeElementString ("Price", "7.99");

MyXmlTextWriter.writeEndelement ();

MyXmlTextWriter.writeEndelement ();

You can create an XML attribute with the WriteAttributeString method. This method uses the name of the attribute and its value as a parameter: Visual Basic MyXMLTextWriter.writestrtElement ("BOOK")

MyXMLTextWriter.WriteAttributeString ("Genre", "Autobiography") MyXmlTextWriter.writeAttributeString ("PublicationDate", "1979")

MyXMLTextWriter.writeAttributeString ("ISBN", "0-7356-0562-9")

MyXmlTextWriter.writeEndelement () C # myXmlTextWriter.writestartElement ("Book", NULL)

MyXMLTextWriter.WriteAttributeString ("Genre", "Autobiography");

MyXMLTextWriter.writeAttributeString ("PublicationDate", "1979");

MYXMLTEXTWRITER.WRITEATTRIBUTESTER.WRITEATTRIBUTESTRING ("ISBN", "0-7356-0562-9");

MyXMLTextWriter.WriteEndelement (); You can choose to use the Flush method to reserve XML to a file: Visual Basic MyXMLTextWriter.Flush () c # myXmlTextWriter.Flush (); Use the Close method to keep XML to a file and close this file: Visual Basic MyXMLTextWriter.Close () c # myxmltextwriter.close (); for your convenience, the following is available: Visual Basic 'Visual Basic .NET

Imports system.xml

Module Module1

Sub main ()

Dim myxmltextwriter as xmltextwriter = new xmltextwriter ("newbooks.xml", system.text.Encoding.utf8)

MyXmlTextWriter.Formatting = system.xml.formatting.indenTED

MyXmlTextWriter.writestartDocument (false)

MyXmlTextWriter.writeComment ("this is a comment")

'Create The Main Document Element.

MyXMLTextWriter.writestartElement ("BookStore")

MyXMLTextWriter.writestartElement ("Book")

'Create An Element Named' Title 'with a TEXT NODE

'and the close the element.

MyXMLTextWriter.writestartElement ("Title")

MyXmlTextWriter.writestring ("The Autobiography of Mark Twain)

MyXmlTextWriter.writeEndelement ()

'Create an element named' author '.

MyXmlTextWriter.writestartElement ("author")

'Create An Element Named' First Name 'with a Text Node' and Close It in ONE LINE.

MyXMLTextWriter.writeElementString ("First-Name", "Mark")

'Create an element named' first-name 'with a text node.

MyXmlTextWriter.writeElementString ("Last-Name", "TWAIN")

'Close Off The Parent Element.

MyXmlTextWriter.writeEndelement ()

'Create An Element Named' Price 'with a Text Node

'and close it in one line.

MyXMLTextWriter.writeElementstring ("Price", "7.99")

'Close off.

MyXmlTextWriter.writeEndelement ()

MyXMLTextWriter.writestartElement ("Book")

MyXMLTextWriter.writeAttributeString ("Genre", "Autobiography")

MyXmlTextWriter.writeAttributeString ("PublicationDate", "1979")

MyXMLTextWriter.writeAttributeString ("ISBN", "0-7356-0562-9")

'Close off.

MyXmlTextWriter.writeEndelement ()

'Close Off The Parent Element Bookstore.

MyXmlTextWriter.writeEndelement ()

MyXmlTextWriter.flush ()

MyXmlTextWriter.Close ()

'Waits for User to Press Enter Before Exiting The Program.

Console.readline ()

End Sub

End module

C # // C #

Using system;

USING SYSTEM.XML;

Namespace Writexmltofile

{

///

/// summary description for class1.

///

Class class1

{

Static void main (string [] args)

{

XmlTextWriter MyXmlTextWriter = New XMLTextWriter ("NewBooks.xml", NULL);

MyXMLTextWriter.Formatting = formatting.indented;

MyXMLTextWriter.writestartDocument (false);

MyXMLTextWriter.writeComment ("this is a comment");

MyXMLTextWriter.writestartElement ("BookStore");

MyXMLTextWriter.writestartElement ("Book", null; MyXMLTextWriter.writeElementString ("Title", Null, "The Autobiography of Mark Twain);

MyXMLTextWriter.writestartElement ("Author", null;

MyXMLTextWriter.writeElementString ("first-name", "mark");

MyXMLTextWriter.writeElementsTRING ("Last-Name", "TWAIN");

MyXmlTextWriter.writeEndelement ();

MyXMLTextWriter.writeElementString ("Price", "7.99");

MyXmlTextWriter.writeEndelement ();

MyXmlTextWriter.flush ();

MyXMLTextWriter.writestartElement ("BOOK", NULL);

MyXMLTextWriter.WriteAttributeString ("Genre", "Autobiography");

MyXMLTextWriter.writeAttributeString ("PublicationDate", "1979");

MYXMLTEXTWRITER.WRITEATTRIBUTESTER.WRITEATTRIBUTESTRING ("ISBN", "0-7356-0562-9");

MyXmlTextWriter.writeEndelement ();

MyXmlTextWriter.writeEndelement ();

MyXmlTextWriter.flush ();

MyXmlTextWriter.Close ();

Console.readline ();

}

}

}

Generate and run your project.

Back to top

Refer to XML IN .NET: .NET Framework XML Classes and C # Offer Simple, Scalable Data Manipulation (XML in .NET: .NET Framework XML class and C # provide simple, scalable data operations) (see MSDN Magazine):

http://msdn.microsoft.com/msdnmag/issues/01/01/xml/xml.asp XMLWRITER class (Microsoft .NET Framework class)

XMLWRITER's function (Microsoft .NET Framework Development Guide)

Back to top

The information in this article applies to:

Microsoft Visual Studio .NET (2002) Professional

Recent Update: 2002-1-15 (1.0) Keyword Kbhowto KbhowTomaster Kbxml Tslic_tslic KB301282 KBAUDDEVELOPER

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

New Post(0)