System.Xml.XmlTextWriter.WriteStartElement Method

Writes the specified start tag and associates it with the given namespace and prefix.

Syntax

public override void WriteStartElement (string prefix, string localName, string ns)

Parameters

prefix
The namespace prefix of the element.
prefix
The namespace prefix of the element.
localName
The local name of the element.
ns
The namespace URI to associate with the element. If this namespace is already in scope and has an associated prefix then the writer automatically writes that prefix also.

Exceptions

TypeReason
ArgumentException XmlTextWriter.Namespaces is false for the writer, and prefix and ns are not both null.
InvalidOperationExceptionThe XmlTextWriter.WriteState is WriteState.Closed.

Remarks

Note:

In the dnprdnext release, the recommended practice is to create System.Xml.XmlWriter instances using the erload:System.Xml.XmlWriter.Create method and the System.Xml.XmlWriterSettings class. This allows you to take full advantage of all the new features introduced in this release. For more information, see Creating XML Writers.

After calling this method you can either write attributes or create content using XmlTextWriter.WriteComment(string), XmlTextWriter.WriteString(string), or XmlTextWriter.WriteStartElement(string, string, string) for child elements. You can close the element with XmlTextWriter.WriteEndElement at which time an end tag is written out.

Example

This example demonstrates the XmlTextWriter.WriteStartElement(string, string, string) method, writing the XML to the console.

C# Example

using System;
using System.Xml;

public class WriteXml 
{
  public static void Main() 
  {
    XmlTextWriter xWriter =
      new XmlTextWriter(Console.Out);
    xWriter.WriteStartDocument();
    xWriter.WriteStartElement("prefix","element", "namespace");
    xWriter.WriteEndDocument();
  }
}

The output is

<?xml version="1.0" encoding= "someencoding"?>

<prefix:element xmlns:prefix="namespace" />

The value of the encoding attribute is the encoding of the output stream of the console.

Requirements

Namespace: System.Xml
Assembly: System.Xml (in System.Xml.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0