XmlTextWriter.WriteStartElement Method (String, String, String)
Writes the specified start tag and associates it with the given namespace and prefix.
Assembly: System.Xml (in System.Xml.dll)
Parameters
- prefix
- Type: System.String
The namespace prefix of the element.
- localName
- Type: System.String
The local name of the element.
- ns
- Type: System.String
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.
| Exception | Condition |
|---|---|
| InvalidOperationException |
The writer is closed. |
Note
|
|---|
|
In the .NET Framework version 2.0 release, the recommended practice is to create XmlWriter instances using the XmlWriter.Create method and the 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 WriteComment, WriteString, or WriteStartElement for child elements. You can close the element with WriteEndElement at which time an end tag is written out.
The following example writes out a book.
using System; using System.IO; using System.Xml; public class Sample { private const string filename = "sampledata.xml"; public static void Main() { XmlTextWriter writer = new XmlTextWriter (filename, null); //Use indenting for readability. writer.Formatting = Formatting.Indented; writer.WriteComment("sample XML fragment"); //Write an element (this one is the root). writer.WriteStartElement("bookstore"); //Write the namespace declaration. writer.WriteAttributeString("xmlns", "bk", null, "urn:samples"); writer.WriteStartElement("book"); //Lookup the prefix and then write the ISBN attribute. string prefix = writer.LookupPrefix("urn:samples"); writer.WriteStartAttribute(prefix, "ISBN", "urn:samples"); writer.WriteString("1-861003-78"); writer.WriteEndAttribute(); //Write the title. writer.WriteStartElement("title"); writer.WriteString("The Handmaid's Tale"); writer.WriteEndElement(); //Write the price. writer.WriteElementString("price", "19.95"); //Write the style element. writer.WriteStartElement(prefix, "style", "urn:samples"); writer.WriteString("hardcover"); writer.WriteEndElement(); //Write the end tag for the book element. writer.WriteEndElement(); //Write the close tag for the root element. writer.WriteEndElement(); //Write the XML to file and close the writer. writer.Flush(); writer.Close(); //Read the file back in and parse to ensure well formed XML. XmlDocument doc = new XmlDocument(); //Preserve white space for readability. doc.PreserveWhitespace = true; //Load the file doc.Load(filename); //Write the XML content to the console. Console.Write(doc.InnerXml); } }
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Note