XmlNode.Attributes 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得包含這個節點屬性 (Attribute) 的 XmlAttributeCollection。
public:
virtual property System::Xml::XmlAttributeCollection ^ Attributes { System::Xml::XmlAttributeCollection ^ get(); };
public virtual System.Xml.XmlAttributeCollection Attributes { get; }
public virtual System.Xml.XmlAttributeCollection? Attributes { get; }
member this.Attributes : System.Xml.XmlAttributeCollection
Public Overridable ReadOnly Property Attributes As XmlAttributeCollection
屬性值
XmlAttributeCollection
,包含這個節點的屬性。
如果這個節點的類型為 XmlNodeType.Element,則傳回這個節點的屬性。 否則,這個函式會傳回 null
。
範例
下列範例會將新的屬性新增至 XML 檔。
#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:ISBN='1-861001-57-5'>"
"<title>Pride And Prejudice</title>"
"</book>" );
XmlNode^ root = doc->FirstChild;
//Create a new attribute.
String^ ns = root->GetNamespaceOfPrefix( "bk" );
XmlNode^ attr = doc->CreateNode( XmlNodeType::Attribute, "genre", ns );
attr->Value = "novel";
//Add the attribute to the document.
root->Attributes->SetNamedItem( attr );
Console::WriteLine( "Display the modified XML..." );
doc->Save( Console::Out );
}
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'>" +
"<title>Pride And Prejudice</title>" +
"</book>");
XmlNode root = doc.FirstChild;
//Create a new attribute.
string ns = root.GetNamespaceOfPrefix("bk");
XmlNode attr = doc.CreateNode(XmlNodeType.Attribute, "genre", ns);
attr.Value = "novel";
//Add the attribute to the document.
root.Attributes.SetNamedItem(attr);
Console.WriteLine("Display the modified XML...");
doc.Save(Console.Out);
}
}
Option Strict
Option Explicit
Imports System.IO
Imports System.Xml
Public Class Sample
Public Shared Sub Main()
Dim doc As New XmlDocument()
doc.LoadXml("<book xmlns:bk='urn:samples' bk:ISBN='1-861001-57-5'>" & _
"<title>Pride And Prejudice</title>" & _
"</book>")
Dim root As XmlNode = doc.FirstChild
'Create a new attribute.
Dim ns As String = root.GetNamespaceOfPrefix("bk")
Dim attr As XmlNode = doc.CreateNode(XmlNodeType.Attribute, "genre", ns)
attr.Value = "novel"
'Add the attribute to the document.
root.Attributes.SetNamedItem(attr)
Console.WriteLine("Display the modified XML...")
doc.Save(Console.Out)
End Sub
End Class