XmlNode.Value 屬性

定義

取得或設定節點的值。

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

屬性值

傳回值需視節點的 NodeType 而定:

類型
屬性 屬性的值。
CDATASection CDATA 區段的內容。
註解 註解的內容。
文件null.
DocumentFragmentnull.
DocumentTypenull.
項目null. 您可以使用 InnerTextInnerXml 屬性,存取項目節點的值。
單位null.
EntityReferencenull.
表示法null.
ProcessingInstruction 除了目標之外的完整內容。
Text 文字節點的內容。
SignificantWhitespace 空白字元。 空白字元可以包含一或多個空字元、歸位字元、換行字元或定位字元。
空白 空白字元。 空白字元可以包含一或多個空字元、歸位字元、換行字元或定位字元。
XmlDeclaration 宣告的內容 (,也就是 ?xml 與 ? >) 之間的 < 一切。

例外狀況

設定唯讀節點的值。

設定不應該有值的節點值 (例如,Element 節點)。

範例

下列範例會將新的屬性新增至 XML 檔,並設定 Value 新屬性的 屬性。

#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

適用於