Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

XmlWriter.WriteAttributeString Method

When overridden in a derived class, writes an attribute with the specified value.

Overload List

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

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Sub WriteAttributeString(String, String)
[C#] public void WriteAttributeString(string, string);
[C++] public: void WriteAttributeString(String*, String*);
[JScript] public function WriteAttributeString(String, String);

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

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Sub WriteAttributeString(String, String, String)
[C#] public void WriteAttributeString(string, string, string);
[C++] public: void WriteAttributeString(String*, String*, String*);
[JScript] public function WriteAttributeString(String, String, String);

When overridden in a derived class, writes out the attribute with the specified prefix, local name, namespace URI, and value.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Sub WriteAttributeString(String, String, String, String)
[C#] public void WriteAttributeString(string, string, string, string);
[C++] public: void WriteAttributeString(String*, String*, String*, String*);
[JScript] public function WriteAttributeString(String, String, String, String);

Example

[Visual Basic, C#, C++] The following example writes out a portion of an XML Schema definition language (XSD) Schema.

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of WriteAttributeString. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
Option Explicit
Option Strict

Imports System
Imports System.IO
Imports System.Xml

Public Class Sample
    Private Shared filename As String = "sampledata.xml"
    Public Shared Sub Main()
        Dim writer As XmlTextWriter = Nothing
        
        writer = New XmlTextWriter(filename, Nothing)
        ' Use indenting for readability.
        writer.Formatting = Formatting.Indented
        
        ' Write the root element.
        writer.WriteStartElement("schema")
        
        ' Write the namespace declarations.
        writer.WriteAttributeString("xmlns", Nothing, "http://www.w3.org/2001/XMLSchema")
        writer.WriteAttributeString("xmlns", "po", Nothing, "http://contoso.com/po")
        
        writer.WriteStartElement("element")
        
        writer.WriteAttributeString("name", "purchaseOrder")
        
        ' Write the type attribute.
        writer.WriteStartAttribute(Nothing, "type", Nothing)
        writer.WriteQualifiedName("PurchaseOrder", "http://contoso.com/po")
        writer.WriteEndAttribute()
        
        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.
        Dim doc As 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)
    End Sub 'Main 
End Class 'Sample


[C#] 
using System;
using System.IO;
using System.Xml;

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

  public static void Main()
  {
     XmlTextWriter writer = null;

     writer = new XmlTextWriter (filename, null);
     // Use indenting for readability.
     writer.Formatting = Formatting.Indented;
        
     // Write the root element.
     writer.WriteStartElement("schema");

     // Write the namespace declarations.
     writer.WriteAttributeString("xmlns", null,"http://www.w3.org/2001/XMLSchema");
     writer.WriteAttributeString("xmlns","po",null,"http://contoso.com/po");

     writer.WriteStartElement("element");

     writer.WriteAttributeString("name", "purchaseOrder");

     // Write the type attribute.
     writer.WriteStartAttribute(null,"type", null);
     writer.WriteQualifiedName("PurchaseOrder", "http://contoso.com/po");
     writer.WriteEndAttribute();

     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);

  }

}

[C++] 
#using <mscorlib.dll>
#using <System.Xml.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml;


int main()
{
     XmlTextWriter* writer = 0;
     String* filename = S"sampledata.xml";

     writer = new XmlTextWriter (filename, 0);
     // Use indenting for readability.
     writer->Formatting = Formatting::Indented;
        
     // Write the root element.
     writer->WriteStartElement(S"schema");

     // Write the namespace declarations.
     writer->WriteAttributeString(S"xmlns", 0,S"http://www.w3.org/2001/XMLSchema");
     writer->WriteAttributeString(S"xmlns",S"po",0,S"http://contoso.com/po");

     writer->WriteStartElement(S"element");

     writer->WriteAttributeString(S"name", S"purchaseOrder");

     // Write the type attribute.
     writer->WriteStartAttribute(0,S"type", 0);
     writer->WriteQualifiedName(S"PurchaseOrder", S"http://contoso.com/po");
     writer->WriteEndAttribute();

     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);

}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

XmlWriter Class | XmlWriter Members | System.Xml Namespace

Show:
© 2015 Microsoft