Export (0) Print
Expand All
0 out of 2 rated this helpful - Rate this topic

XmlWriter.WriteElementString Method (String, String)

When overridden in a derived class, writes an element with the specified local name and value.

Namespace:  System.Xml
Assembly:  System.Xml (in System.Xml.dll)
public void WriteElementString(
	string localName,
	string value
)

Parameters

localName
Type: System.String

The local name of the element.

value
Type: System.String

The value of the element.

ExceptionCondition
ArgumentException

The localName value is null or an empty string.

-or-

The parameter values are not valid.

EncoderFallbackException

There is a character in the buffer that is a valid XML character but is not valid for the output encoding. For example, if the output encoding is ASCII, you should only use characters from the range of 0 to 127 for element and attribute names. The invalid character may be in the argument of this method or in an argument of previous methods that were writing to the buffer. Such characters are escaped by character entity references when possible (for example, in text nodes or attribute values). However, the character entity reference is not allowed in element and attribute names, comments, processing instruction, or CDATA sections. If a character not valid for the output encoding is encountered, an EncoderFallbackException is thrown.

The following example uses several write methods to create an XML fragment.

using System;
using System.IO;
using System.Xml;

 public class Sample
 {
   private const string m_Document = "sampledata.xml";

   public static void Main() {

      XmlWriter writer = null;

      try {

        XmlWriterSettings settings = new XmlWriterSettings();
        settings.Indent = true;
        writer = XmlWriter.Create (m_Document, settings);

        writer.WriteComment("sample XML fragment");

        // Write an element (this one is the root).
        writer.WriteStartElement("book");

        // Write the namespace declaration.
        writer.WriteAttributeString("xmlns", "bk", null, "urn:samples");

        // Write the genre attribute.
        writer.WriteAttributeString("genre", "novel");

        // Write the title.
        writer.WriteStartElement("title");
        writer.WriteString("The Handmaid's Tale");
        writer.WriteEndElement();

        // Write the price.
        writer.WriteElementString("price", "19.95");

        // Lookup the prefix and write the ISBN element.
        string prefix = writer.LookupPrefix("urn:samples");
        writer.WriteStartElement(prefix, "ISBN", "urn:samples");
        writer.WriteString("1-861003-78");
        writer.WriteEndElement();

        // Write the style element (shows a different way to handle prefixes).
        writer.WriteElementString("style", "urn:samples", "hardcover");

        // Write the close tag for the root element.
        writer.WriteEndElement();

        // Write the XML to file and close the writer.
        writer.Flush();
        writer.Close();
      }

      finally {
        if (writer != null)
           writer.Close();
     } 
   }

 }
#using <mscorlib.dll>
#using <System.Xml.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml;

 public __gc class Sample
 {
private:
   static String* m_Document = S"sampledata.xml";

public:
   static void Main() {

      XmlWriter * writer = 0;

      try {

        XmlWriterSettings * settings = new XmlWriterSettings();
        settings->Indent = true;
        writer = XmlWriter::Create (m_Document);

        writer->WriteComment(S"sample XML fragment");

        // Write an element (this one is the root).
        writer->WriteStartElement(S"book");

        // Write the namespace declaration.
        writer->WriteAttributeString(S"xmlns", S"bk", 0, S"urn:samples");

        // Write the genre attribute.
        writer->WriteAttributeString(S"genre", S"novel");

        // Write the title.
        writer->WriteStartElement(S"title");
        writer->WriteString(S"The Handmaid's Tale");
        writer->WriteEndElement();

        // Write the price.
        writer->WriteElementString(S"price", S"19.95");

        // Lookup the prefix and write the ISBN element.
        String* prefix = writer->LookupPrefix(S"urn:samples");
        writer->WriteStartElement(prefix, S"ISBN", S"urn:samples");
        writer->WriteString(S"1-861003-78");
        writer->WriteEndElement();

        // Write the style element (shows a different way to handle prefixes).
        writer->WriteElementString(S"style", S"urn:samples", S"hardcover");

        // Write the close tag for the root element.
        writer->WriteEndElement();

        // Write the XML to file and close the writer.
        writer->Flush();
        writer->Close();
      }

      __finally {
        if (writer != 0)
           writer->Close();
     }
   }
 };

void main(){
	Sample::Main();
}

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.