XmlElement.Prefix Property

Definition

Gets or sets the namespace prefix of this node.

public override string Prefix { get; set; }

Property Value

The namespace prefix of this node. If there is no prefix, this property returns String.Empty.

Exceptions

This node is read-only.

The specified prefix contains an invalid character.

The specified prefix is malformed.

The namespaceURI of this node is null.

The specified prefix is "xml" and the namespaceURI of this node is different from http://www.w3.org/XML/1998/namespace.

Examples

The following example displays information on the ISBN element.

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

public class Sample
{
  public static void Main()
  {

    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<book xmlns:bk='urn:samples'>" +
                "<bk:ISBN>1-861001-57-5</bk:ISBN>" +
                "<title>Pride And Prejudice</title>" +
                "</book>");

    // Display information on the ISBN element.
    XmlElement elem = (XmlElement) doc.DocumentElement.FirstChild;
    Console.Write("{0}:{1} = {2}", elem.Prefix, elem.LocalName, elem.InnerText);
    Console.WriteLine("\t namespaceURI=" + elem.NamespaceURI);
  }
}

// This code produces the following output.
// bk:ISBN = 1-861001-57-5  namespaceURI=urn:samples

Remarks

Setting this property changes the Name property, which holds the qualified name for an XmlElement. However, changing the prefix does not change the namespace URI of the element.

Applies to

Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0