Writing XML in C #

Source: Internet
Author: User
Tags constructor

In my last article, I discussed how to use it. NET framework to read XML in C # and some related concepts of reading, today we'll talk about how to write XML documents in C #, and at first I thought it was a bit of a trouble to write XML in a programmatic way, I thought it would be useful to think about it later, and I think the Microsoft guys could have made these classes just to get the bill I gates done! As for its usefulness ... For example, to do installation program Ah! We can generate the corresponding XML document based on the options that the user chooses during the installation process and some settings to initialize our application based on the XML document. Empty words do not say so much, let's take a look at the specific implementation details.

To write XML is also the concept of streaming, the details of writing XML in. NET are implemented as XmlWriter classes, but the class is not instantiated by an abstract class, so we want to access its methods in the program to implement the desire to write XML, It is necessary to use its derived class XmlTextWriter, which provides a series of properties and methods to prepare us for writing XML, which is described in detail below:

constructor function:

public XmlTextWriter(TextWriter);
public XmlTextWriter(Stream, Encoding);
public XmlTextWriter(string, Encoding);

The first constructor passes the existing TextWriter instance, and the System.IO.TextWriter class is an ordered character stream

The second constructor takes the stream to be written as the first argument, the second parameter is the encoding of the specified XML document, the default is UTF8, the encoding enumeration value, the stream can be filestream,memorystream,networkstream, and so on

The third constructor passes the file name you want to write as a string (overriding the file if it exists) to the first argument, and the second parameter specifies how the encoding

Common methods:

Writerstartdocument () and Writerenddocument () methods:

The first method is used to write the XML declaration section, such as:

The second method is used to close any open elements or attributes and reset the writer to the start state.

Writerstartelement () and WriteEndElement () methods:

The first method is used to write out the specified start tag, which has the following overloads:

WriterStartElement(string localname)

Use the passed string as the local name of the element

WriterStartElement(string localname,string namespace)

The first parameter specifies the local name of the element, and the second parameter specifies the namespace in which the element is located

WriterStartElement(string prefix,string localname,string namespace)

The first parameter specifies the prefix of the element, the second parameter specifies the local name of the element, and the third parameter specifies the namespace in which the element is located

The second method is used to write off the closing element that corresponds to the start element, and if the starting element contains nothing, a "/>" is used as the closing element

Writerstartattribute () and Writerendattribute () methods:

The first method is used to write the beginning of a property that has two overloads:

WriterStartAttribute(string localname,string namespace)

The first parameter specifies the local name of the property, and the second parameter specifies the namespace in which the property is located

WriterStartAttribute(string prefix,string localname,string namespace)

The first parameter specifies the prefix of the property, the second parameter specifies the local name of the property, and the third argument specifies the namespace in which the property is located

The second method is used to turn off the properties created by Writerstartattribute

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.