. A simple method of writing in net

Source: Internet
Author: User
Tags format closing tag contains net processing instruction

XML is a popular technology. One of the main reasons it attracts people's interest is that it's so simple that people can easily understand and use it. Every programmer can easily read an XML file and understand what it contains.

. NET contains many classes that support XML, and these classes make it easy for programmers to use XML to program as well as to understand XML files. In this article, I'll give you an example of the use of a class that is the XmlTextWriter class.

The XmlTextWriter class allows you to write XML to a file. This class contains a number of methods and properties that you can use to make it easier for you to work with XML. In order to use this class, you must first create a new XmlTextWriter object, and then you can add the XML fragment to the object. This class contains a number of methods for adding various types of XML elements to an XML file, and the following table gives the names and descriptions of these methods:

Method

Describe

WriteStartDocument

XML declaration with write version "1.0"

WriteEndDocument

Close any open elements or attributes

Close

Close the stream

Writedoctype

Writes out the DOCTYPE declaration with the specified name and optional attributes

WriteStartElement

Writes out the specified start tag

WriteEndElement

Close an element

Writefullendelement

Closes an element and always writes the complete closing tag

WriteElementString

Write out an element that contains a string value

WriteStartAttribute

Start of writing attribute

WriteEndAttribute

Close the previous WriteStartAttribute call

WriteRaw

Manually write the original markup

WriteString

Write a string

WriteAttributeString

The property with the specified value

Writecdata

Writes the <! containing the specified text [cdata[...]] > Block

WriteComment

Writes a Comment <!--...--> that contains the specified text

Writewhitespace

Write out a given blank

WriteProcessingInstruction

Write out a processing instruction with a space between the name and the text, as follows: <?name text?>


If you are familiar with XML, you will have a good understanding of these methods. Here's an example where we'll create a document, add some elements, and then close the document. After adding elements, you can also add child elements, attributes, and other content. The following code is an example of this, which creates an XML file named title.

Using System;

Using System.IO;

Using System.Xml;

public class Sample

{

public static void Main ()

{

XmlTextWriter writer = new XmlTextWriter ("Titles.XML", null);

Writing to the root element

Writer. WriteStartElement ("items");

Adding child elements

Writer. WriteElementString ("title", "Unreal Tournament 2003");

Writer. WriteElementString ("title", "C&c:renegade");

Writer. WriteElementString ("title", "Dr Seuss's ABC");

Close the root element and write the end tag

Writer. WriteEndElement ();

Write XML to file and close XmlTextWriter

Writer. Close ();

}

}

If you compile and execute the above code, you will create the XML file that contains the following:

<items><title>unreal Tournament 2003</title><title>

C&c:renegade</title><title>dr. Seuss ' s ABC

</title></items>

The code above creates a XmlTextWriter object named writer. When this object is created, it is associated with a file named Titles.XML. Next, the program creates a root property called items, and the WriteStartElement method creates a start tag for the property. Next, the program calls the WriteElementString method to create three child elements. As you can see from the above code, this method uses the first argument (title in the program above) as the label of the element, and the second parameter as the value of the element. When you add all the elements, you need to close the root element. You can then call the WriteEndElement method to close the recently opened element; In this case, the most recently opened element is the root element. When all the data has been written and the root element is closed, you can send the information to your XmlTextWriter. This means that at this point you can call the Close method to turn it off.

The above code is relatively simple, so let's look at an example that uses more methods in the XmlTextWriter class to improve functionality.

Using System;

Using System.IO;

Using System.Xml;

public class Sample

{

public static void Main ()

{

XmlTextWriter writer = new XmlTextWriter ("Mymedia.xml", null);

Easy to read with automatic indentation

Writer. formatting = formatting.indented;

Writing the root element

Writer. WriteStartElement ("items");

Start an element

Writer. WriteStartElement ("item");

Add a property to the previously created element

Writer. WriteAttributeString ("rating", "R");

Adding child elements

Writer. WriteElementString ("title", "The Matrix");

Writer. WriteElementString ("format", "DVD");

Close Item Element

Writer.  WriteEndElement (); Close element

Add a few spaces between nodes

Writer. Writewhitespace ("\ n");

Writing a second element with the original string

Writer. WriteRaw ("<item>" +

"<title>BloodWake</title>" +

"<format>XBox</format>" +

"</item>");

Use a formatted string to write a third element

Writer. WriteRaw ("\ n <item>\n" +

"<title>unreal Tournament 2003</title>\n" +

"<format>cd</format>\n" +

"</item>\n");

Close the root element

Writer. Writefullendelement ();

Write XML to file and close writer

Writer. Close ();

}

}

The above code compiles and runs and gets the Mymedia.xml file, which reads:

<items>

<item rating= "R" >

<title>the matrix</title>

<format>DVD</format>

</item>

<item><title>BloodWake</title><format>XBox</format></item>

<item>

<title>unreal Tournament 2003</title>

<format>CD</format>

</item>

</items>

The comments in the code above illustrate how the program's functionality is implemented. One thing to keep in mind is that when you invoke a method to start an operation, you need to call the method at the appropriate place in the program to end the operation. For example, if you call startelement, you must call the EndElement close element, and of course you can add a child element between the two calls. Whenever you call the EndElement method, it always closes the element that was recently opened using the Startelement method (which is similar to how the stack works).

It's very easy to use XmlTextWriter, but I suggest you try these codes and methods yourself. You'll find that the code can be easily integrated into your program. You should also remember that XmlTextWriter is only one of the many XML classes provided by. Net. Like XmlTextWriter, other XML classes are also very easy to use.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.