Export (0) Print
Expand All

XmlTextWriter.WriteQualifiedName Method

Writes out the namespace-qualified name. This method looks up the prefix that is in scope for the given namespace.

[Visual Basic]
Overrides Public Sub WriteQualifiedName( _
   ByVal localName As String, _
   ByVal ns As String _
)
[C#]
public override void WriteQualifiedName(
 string localName,
 string ns
);
[C++]
public: void WriteQualifiedName(
 String* localName,
 String* ns
);
[JScript]
public override function WriteQualifiedName(
   localName : String,
 ns : String
);

Parameters

localName
The local name to write.
ns
The namespace URI to associate with the name.

Exceptions

Exception Type Condition
ArgumentException localName is either a null reference (Nothing in Visual Basic) or String.Empty.

localName is not a valid name according to the W3C Namespaces spec.

Remarks

For example, the following C# code:

writer.Formatting = Formatting.Indented;
writer.WriteStartElement("root");
 writer.WriteAttributeString("xmlns","x",null,"urn:abc");
 writer.WriteStartElement("item");
 writer.WriteStartAttribute("href",null);
 writer.WriteString("#");
 writer.WriteQualifiedName("test","urn:abc");
 writer.WriteEndAttribute();
 writer.WriteEndElement();
 writer.WriteEndElement();
 writer.Close();
 

Generates the following output:

<root xmlns:x="urn:abc">
 <item href="#x:test"/>
 </root>
 

If ns maps to the current default namespace, no prefix is generated.

When writing attribute values, this method generates a prefix if ns is not found. When writing element content, it throws an exception if ns is not found.

If this writer supports namespaces (Namespaces is set to true), this method also checks that the name is valid according to the W3C Namespaces in XML recommendation (http://www.w3.org/TR/REC-xml-names).

Example

[Visual Basic, C#, C++] The following example writes out a portion of a XSD schema.

[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.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

XmlTextWriter Class | XmlTextWriter Members | System.Xml Namespace

Show:
© 2014 Microsoft