共用方式為


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
文件片段 null
DocumentType null
元素 null。 你可以使用 InnerText or InnerXml 屬性來存取元素節點的值。
實體 null
實體參考 null
符號 null
處理指令 除了目標之外,整個內容。
文字 文字節點的內容。
顯著空白 空白字元。 空白部分可以包含一個或多個空格字元、回車、換行或制表表。
空白空間 空白字元。 空白部分可以包含一個或多個空格字元、回車、換行或制表表。
XmlDeclaration 宣告內容(即 ?xml 到 ?> 之間的所有<內容)。

例外狀況

設定只讀節點的值。

設定一個不應該有值的節點值(例如元素節點)。

範例

以下範例為 XML 文件新增屬性,並設定 Value 該新屬性的屬性。

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

適用於