XmlAttribute.Prefix 屬性

定義

取得或設定這個節點的命名空間前置詞。

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

這個節點的命名空間前置詞。 如果沒有前置詞,則這個屬性會傳回 String.Empty。

例外狀況

這個節點是唯讀的。

指定的前置詞包含無效的字元。

指定的前置詞格式不正確。

此節點的 namespaceURI 為 null

指定的前置詞為 "xml",且此節點的 namespaceURI 與 "http://www.w3.org/XML/1998/namespace" (英文) 的不同。

此節點是一個屬性,指定的前置詞是 "xmlns",且此節點的 namespaceURI 與 "http://www.w3.org/2000/xmlns/" (英文) 的不同。

這個節點是一個屬性,其 qualifiedName 為 "xmlns" [Namespaces]。

範例

下列範例會顯示內容集合中每個節點的資訊。

#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,所以變更已知具有預設值的屬性前置詞並不會建立具有預設值和原始前置詞的新屬性。

適用於