XmlNode.Value 屬性

定義

取得或設定節點的值。Gets or sets the value of the node.

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

屬性值

傳回值需視節點的 NodeType 而定:The value returned depends on the NodeType of the node:

類型Type Value
屬性Attribute 屬性的值。The value of the attribute.
CDATASectionCDATASection CDATA 區段的內容。The content of the CDATA Section.
註解Comment 註解的內容。The content of the comment.
文件Document null.null.
DocumentFragmentDocumentFragment null.null.
DocumentTypeDocumentType null.null.
項目Element null.null. 您可以使用 InnerTextInnerXml 屬性,存取項目節點的值。You can use the InnerText or InnerXml properties to access the value of the element node.
實體Entity null.null.
EntityReferenceEntityReference null.null.
NotationNotation null.null.
ProcessingInstructionProcessingInstruction 除了目標之外的完整內容。The entire content excluding the target.
TextText 文字節點的內容。The content of the text node.
SignificantWhitespaceSignificantWhitespace 空白字元。The white space characters. 空白字元可以包含一或多個空字元、歸位字元、換行字元或定位字元。White space can consist of one or more space characters, carriage returns, line feeds, or tabs.
WhitespaceWhitespace 空白字元。The white space characters. 空白字元可以包含一或多個空字元、歸位字元、換行字元或定位字元。White space can consist of one or more space characters, carriage returns, line feeds, or tabs.
XmlDeclarationXmlDeclaration 宣告的內容(也就是 <? xml 與?>之間的所有專案)。The content of the declaration (that is, everything between <?xml and ?>).

例外狀況

設定唯讀節點的值。Setting the value of a node that is read-only.

設定不應該有值的節點值 (例如,Element 節點)。Setting the value of a node that is not supposed to have a value (for example, an Element node).

範例

下列範例會將新的屬性加入至 XML 檔,並設定新屬性的 Value 屬性。The following example adds a new attribute to the XML document and sets the Value property of the new attribute.

#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

適用於