XmlAttribute.Prefix 属性

定义

获取或设置该节点的命名空间前缀。Gets or sets the namespace prefix of this node.

public:
 virtual property System::String ^ Prefix { System::String ^ get(); void set(System::String ^ value); };
public override string Prefix { get; set; }
member this.Prefix : string with get, set
Public Overrides Property Prefix As String

属性值

String

该节点的命名空间前缀。The namespace prefix of this node. 如果没有前缀,则该属性返回 String.Empty。If there is no prefix, this property returns String.Empty.

例外

该节点是只读的。This node is read-only.

指定的前缀包含无效字符。The specified prefix contains an invalid character.

指定的前缀格式不正确。The specified prefix is malformed.

该节点的 namespaceURI 为 nullThe namespaceURI of this node is null.

指定的前缀为“xml”,而该节点的 namespaceURI 与“http://www.w3.org/XML/1998/namespace”不同。The specified prefix is "xml", and the namespaceURI of this node is different from "http://www.w3.org/XML/1998/namespace".

该节点是一个属性,指定的前缀为“xmlns”,且该节点的 namespaceURI 与“http://www.w3.org/2000/xmlns/”不同。This node is an attribute, the specified prefix is "xmlns", and the namespaceURI of this node is different from "http://www.w3.org/2000/xmlns/".

该节点是一个特性,并且该节点的 qualifiedName 是“xmlns”[Namespaces]。This node is an attribute, and the qualifiedName of this node is "xmlns" [Namespaces].

示例

下面的示例显示了有关特性集合中的每个节点的信息。The following example displays information on each of the nodes in the attribute collection.

#using <System.Xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
int main()
{
   XmlDocument^ doc = gcnew XmlDocument;
   doc->LoadXml( "<book xmlns:bk='urn:samples' bk:genre='novel'><title>Pride And Prejudice</title></book>" );
   
   //Create an attribute collection.
   XmlAttributeCollection^ attrColl = doc->DocumentElement->Attributes;
   Console::WriteLine( "Display information on each of the attributes... \r\n" );
   System::Collections::IEnumerator^ myEnum = attrColl->GetEnumerator();
   while ( myEnum->MoveNext() )
   {
      XmlAttribute^ attr = safe_cast<XmlAttribute^>(myEnum->Current);
      Console::Write( "{0}:{1} = {2}", attr->Prefix, attr->LocalName, attr->Value );
      Console::WriteLine( "\t namespaceURI={0}", attr->NamespaceURI );
   }
}

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:genre='novel'>" +
                "<title>Pride And Prejudice</title>" +
                "</book>");

    //Create an attribute collection.
    XmlAttributeCollection attrColl = doc.DocumentElement.Attributes;

    Console.WriteLine("Display information on each of the attributes... \r\n");
    foreach (XmlAttribute attr in attrColl){
       Console.Write("{0}:{1} = {2}", attr.Prefix, attr.LocalName, attr.Value);
       Console.WriteLine("\t namespaceURI=" + attr.NamespaceURI);
    }
  }
}
Imports System.IO
Imports System.Xml

public class Sample

  public shared sub Main()

    Dim doc as XmlDocument = new XmlDocument()
    doc.LoadXml("<book xmlns:bk='urn:samples' bk:genre='novel'>" & _
                "<title>Pride And Prejudice</title>" & _
                "</book>") 

    'Create an attribute collection.
    Dim attrColl as XmlAttributeCollection = doc.DocumentElement.Attributes

    Console.WriteLine("Display information on each of the attributes... ")
    Dim attr as XmlAttribute
    for each attr in attrColl
       Console.Write("{0}:{1} = {2}", attr.Prefix, attr.LocalName, attr.Value)
       Console.WriteLine("   namespaceURI=" + attr.NamespaceURI)
    next

  end sub
end class

注解

由于更改属性的前缀不会更改其命名空间 URI,因此更改已知具有默认值的属性的前缀不会创建具有默认值和原始前缀的新属性。Because changing the prefix of an attribute does not change its namespace URI, changing the prefix of an attribute that is known to have a default value does not create a new attribute with the default value and the original prefix.

适用于