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

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)

'Declaration
Public Overrides Sub WriteQualifiedName ( _
	localName As String, _
	ns As String _
)

Parameters

localName
Type: System.String

The local name to write.

ns
Type: System.String

The namespace URI to associate with the name.

ExceptionCondition
ArgumentException

localName is either Nothing or String.Empty.

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

NoteNote

Starting with the .NET Framework 2.0, we recommend that you create XmlWriter instances by using the XmlWriter.Create method and the XmlWriterSettings class to take advantage of new functionality.

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.

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

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

XNA Framework

Supported in: 3.0, 2.0, 1.0
Show:
© 2015 Microsoft