XmlReader.Value XmlReader.Value XmlReader.Value XmlReader.Value Property

定义

当在派生类中被重写时,获取当前节点的文本值。When overridden in a derived class, gets the text value of the current node.

public:
 abstract property System::String ^ Value { System::String ^ get(); };
public abstract string Value { get; }
member this.Value : string
Public MustOverride ReadOnly Property Value As String

属性值

返回的值取决于节点的 NodeTypeThe value returned depends on the NodeType of the node. 下表列出具有要返回的值的节点类型。The following table lists node types that have a value to return. 所有其他节点类型返回 String.EmptyAll other node types return String.Empty.

节点类型Node type Value
Attribute 属性的值。The value of the attribute.
CDATA CDATA 节的内容。The content of the CDATA section.
Comment 注释的内容。The content of the comment.
DocumentType 内部子集。The internal subset.
ProcessingInstruction 全部内容(不包括指令目标)。The entire content, excluding the target.
SignificantWhitespace 混合内容模型中标记之间的空白。The white space between markup in a mixed content model.
Text 文本节点的内容。The content of the text node.
Whitespace 标记之间的空白。The white space between markup.
XmlDeclaration 声明的内容。The content of the declaration.

异常

在上一次异步操作完成之前调用了 XmlReader 方法。An XmlReader method was called before a previous asynchronous operation finished. 在此情况下,会引发 InvalidOperationException 并显示消息“异步操作已在进行中。”In this case, InvalidOperationException is thrown with the message "An asynchronous operation is already in progress."

示例

下面的示例读取 XML 文件,并显示每个节点。The following example reads an XML file and displays each of the nodes.

XmlReaderSettings settings = new XmlReaderSettings();
settings.DtdProcessing = DtdProcessing.Parse;
XmlReader reader = XmlReader.Create("items.xml", settings);
  
reader.MoveToContent();
  // Parse the file and display each of the nodes.
  while (reader.Read()) {
    switch (reader.NodeType) {
      case XmlNodeType.Element:
          Console.Write("<{0}>", reader.Name);
          break;
      case XmlNodeType.Text:
          Console.Write(reader.Value);
          break;
       case XmlNodeType.CDATA:
           Console.Write("<![CDATA[{0}]]>", reader.Value);
           break;
       case XmlNodeType.ProcessingInstruction:
           Console.Write("<?{0} {1}?>", reader.Name, reader.Value);
           break;
       case XmlNodeType.Comment:
           Console.Write("<!--{0}-->", reader.Value);
           break;
       case XmlNodeType.XmlDeclaration:
           Console.Write("<?xml version='1.0'?>");
           break;
       case XmlNodeType.Document:
           break;
       case XmlNodeType.DocumentType:
           Console.Write("<!DOCTYPE {0} [{1}]", reader.Name, reader.Value);
           break;
       case XmlNodeType.EntityReference:
           Console.Write(reader.Name);
           break;
       case XmlNodeType.EndElement:
           Console.Write("</{0}>", reader.Name);
           break;
   } 
}
Dim settings As New XmlReaderSettings()
settings.DtdProcessing = DtdProcessing.Parse
Dim reader As XmlReader = XmlReader.Create("items.xml", settings)
reader.MoveToContent()
' Parse the file and display each of the nodes.
While reader.Read()
  Select Case reader.NodeType
    Case XmlNodeType.Element
      Console.Write("<{0}>", reader.Name)
    Case XmlNodeType.Text
      Console.Write(reader.Value)
    Case XmlNodeType.CDATA
      Console.Write("<![CDATA[{0}]]>", reader.Value)
    Case XmlNodeType.ProcessingInstruction
      Console.Write("<?{0} {1}?>", reader.Name, reader.Value)
    Case XmlNodeType.Comment
      Console.Write("<!--{0}-->", reader.Value)
    Case XmlNodeType.XmlDeclaration
      Console.Write("<?xml version='1.0'?>")
    Case XmlNodeType.Document
    Case XmlNodeType.DocumentType
      Console.Write("<!DOCTYPE {0} [{1}]", reader.Name, reader.Value)
    Case XmlNodeType.EntityReference
      Console.Write(reader.Name)
    Case XmlNodeType.EndElement
      Console.Write("</{0}>", reader.Name)
  End Select
End While

此示例使用items.xml文件。The sample uses the items.xml file.

<?xml version="1.0"?>
<!-- This is a sample XML document -->
<!DOCTYPE Items [<!ENTITY number "123">]>
<Items>
  <Item>Test with an entity: &number;</Item>
  <Item>Test with a child element <more/> stuff</Item>
  <Item>Test with a CDATA section <![CDATA[<456>]]> def</Item>
  <Item>Test with a char entity: &#65;</Item>
  <!-- Fourteen chars in this element.-->
  <Item>1234567890ABCD</Item>
</Items>

注解

此属性的异步版本,请参阅GetValueAsync方法。For the asynchronous version of this property, see the GetValueAsync method.

适用于