XmlNode.Attributes 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取一个 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