XmlNode.Value Właściwość

Definicja

Pobiera lub ustawia wartość węzła.

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

Wartość właściwości

Zwrócona wartość zależy od NodeType węzła:

Typ Wartość
Atrybut Wartość atrybutu.
CDATASection Zawartość sekcji CDATA.
Komentarz Zawartość komentarza.
Dokumentnull.
Documentfragmentnull.
Documenttypenull.
Elementnull. Możesz użyć InnerText właściwości or InnerXml , aby uzyskać dostęp do wartości węzła elementu.
Jednostkanull.
Odwołanie do jednostkinull.
Notacjanull.
ProcessingInstruction Cała zawartość wykluczająca element docelowy.
Tekst Zawartość węzła tekstowego.
ZnacząceWhitespace Znaki odstępu. Odstęp może składać się z co najmniej jednego znaku spacji, powrotu karetki, kanałów informacyjnych lub kart.
Białe znaki Znaki odstępu. Odstęp może składać się z co najmniej jednego znaku spacji, powrotu karetki, kanałów informacyjnych lub kart.
Xmldeclaration Zawartość deklaracji (czyli wszystko między <?xml i ?>).

Wyjątki

Ustawianie wartości węzła, który jest tylko do odczytu.

Ustawianie wartości węzła, który nie ma mieć wartości (na przykład węzła elementu).

Przykłady

Poniższy przykład dodaje nowy atrybut do dokumentu XML i ustawia Value właściwość nowego atrybutu.

#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

Dotyczy