C # XML programming for the warm and newest Learning Series-Xml writer XmlWriter class (3)

Source: Internet
Author: User

  Preface

Corresponds to the XmlReader class ,. NETFramework also provides a fast, non-cached, forward-only, and dynamically written XML data class, that is, the XmlWriter class, which can be understood as a class equivalent to the XmlReader class.

Reading directory

I. Steps for writing an XML document

II. Implementation steps

I. Steps for writing an XML document

To write an attribute, you must call the WriteStrat method and WriteEnd () method. When using the XmlWriter class, you do not simply write an element, you must first write the start tag, then write the content, and finally write the end tag. Therefore, you must track the location in the XML document to ensure that the correct end method is called at the right time.

1: Use the Create () method of the XmlWriter class to Create an instance of this class, and use the XML document name as a parameter to pass in the Method

2: Start document

3: Write Start tag

4: write content

5: Write end tag

6. End the document;

Instance

  II. Implementation steps

1: Write an XML file

<? Xml version = "1.0" encoding = "UTF-8"?>

2: code file writing

Form1.cs

Using System;
Using System. Collections. Generic;
Using System. ComponentModel;
Using System. Data;
Using System. Drawing;
Using System. Text;
Using System. Windows. Forms;
Using System. Xml;

Namespace XmlWriterClass
{
Public partial class Form1: Form
{
Public Form1 ()
{
InitializeComponent ();
}

}

Private void Form1_Load (object sender, EventArgs e)
{
XmlWriter xml_doc = XmlWriter. Create ("XmlFile. xml ");
// Method for writing elements
// Write the XML Declaration whose version is 1.0
Xml_doc.WriteStartDocument ();
// Write the element name and its value. Note that this method of writing elements cannot write any attribute on it.
Xml_doc.WriteElementString ("Title", "C # tutorial ");
// Close the document
Xml_doc.WriteEndDocument ();
Xml_doc.Flush ();
Xml_doc.Close ();

        

// Method for writing elements
// Write the XML Declaration whose version is 1.0
Xml_doc.WriteStartDocument ();
// Write the specified start flag. You can write the attribute on the specified element.
Xml_doc.WriteStartElement ("Books ");
// Close the element
Xml_doc.WriteEndElement ();
// Close the document
Xml_doc.WriteEndDocument ();
Xml_doc.Flush ();
Xml_doc.Close ();

        

// First write attribute Method
Xml_doc.WriteStartDocument ();

// Write the <Books/> element
Xml_doc.WriteStartElement ("Books ");

// Write attribute name
Xml_doc.WriteStartAttribute ("issue ");

// Write Attribute Value
Xml_doc.WriteValue ("Tsinghua press ");

// Close the call of the previous xml_doc.WriteStartAttribute ("issue ")
Xml_doc.WriteEndAttribute ();

// Close the <Books/> element
Xml_doc.WriteEndElement ();
Xml_doc.WriteEndDocument ();
Xml_doc.Flush ();
Xml_doc.Close ();

        

// Method 2

Xml_doc.WriteStartDocument ();
Xml_doc.WriteStartElement ("Books ");

// Name and value of the attribute written at one time
Xml_doc.WriteAttributeString ("issue", "Tsinghua press ");
Xml_doc.WriteEndElement ();
Xml_doc.WriteEndDocument ();
Xml_doc.Flush ();
Xml_doc.Close ();

        

// Used in combination

Xml_doc.WriteStartDocument ();
// Write <Books/> elements and attribute values. Here, we do not use the xml_doc.WriteElementString () method to write elements, because we also need to write attributes on the elements we write, while xml_doc.WriteElementString () this method of writing elements cannot write any attribute on it.
Xml_doc.WriteStartElement ("Books ");
Xml_doc.WriteAttributeString ("issue", "Tsinghua press ");
// Write <Titles/> elements and attribute values. Note that we use another method to write attributes.
Xml_doc.WriteStartElement ("Title ");
Xml_doc.WriteStartAttribute ("OthorName ");
Xml_doc.WriteValue ("C # getting started and proficient ");
Xml_doc.WriteEndAttribute ();
Xml_doc.WriteValue ("C # basics ");
Xml_doc.WriteEndElement ();
Xml_doc.WriteEndElement ();
Xml_doc.WriteEndDocument ();
Xml_doc.Flush ();
Xml_doc.Close ();

        

}
}

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.