XmlNode.Attributes Propriété
Définition
Important
Certaines informations portent sur la préversion du produit qui est susceptible d’être en grande partie modifiée avant sa publication. Microsoft exclut toute garantie, expresse ou implicite, concernant les informations fournies ici.
Obtient un XmlAttributeCollection contenant les attributs du nœud.
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
Valeur de propriété
XmlAttributeCollection
contenant les attributs du nœud.
Si le nœud est de type XmlNodeType.Element, les attributs du nœud sont retournés. Sinon, cette propriété retourne null
.
Exemples
L’exemple suivant ajoute un nouvel attribut au document 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