WriteQualifiedName Method

XmlTextWriter.WriteQualifiedName Method

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

Namespace: System.Xml
Assembly: System.Xml (in system.xml.dll)

public override void WriteQualifiedName (
	string localName,
	string ns
)
public void WriteQualifiedName (
	String localName, 
	String ns
)
public override function WriteQualifiedName (
	localName : String, 
	ns : String
)

Parameters

localName

The local name to write.

ns

The namespace URI to associate with the name.

Exception typeCondition

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.

NoteNote

In the Microsoft .NET Framework version 2.0 release, the recommended practice is to create XmlWriter instances using the System.Xml.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.

For example, the following Microsoft Visual 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).

The following example writes out a portion of a XSD schema.

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

  }

}

import System.*;
import System.IO.*;
import System.Xml.*;

public class Sample
{
    private static String fileName = "sampledata.xml";

    public static void main(String[] args)
    {
        XmlTextWriter writer = null;
        writer = new XmlTextWriter(fileName, null);

        // Use indenting for readability.
        writer.set_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.set_PreserveWhitespace(true);

        // Load the file.
        doc.Load(fileName);

        // Write the XML content to the console.
        Console.Write(doc.get_InnerXml());
    } //main 
} //Sample

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

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

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show:
© 2016 Microsoft