通过


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
DocumentFragment null
DocumentType null
元素 null。 可以使用 InnerTextInnerXml 属性访问元素节点的值。
实体 null
EntityReference null
符号 null
ProcessingInstruction 不包括目标的整个内容。
文本 文本节点的内容。
SignificantWhitespace 空格字符。 空格可以包含一个或多个空格字符、回车符、换行符或制表符。
空白 空格字符。 空格可以包含一个或多个空格字符、回车符、换行符或制表符。
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

适用于