XmlReader.ReadInnerXml 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
在派生类中重写时,以字符串形式读取所有内容,包括标记。
public:
virtual System::String ^ ReadInnerXml();
public virtual string ReadInnerXml();
abstract member ReadInnerXml : unit -> string
override this.ReadInnerXml : unit -> string
Public Overridable Function ReadInnerXml () As String
返回
当前节点中的所有 XML 内容(包括标记)。 如果当前节点没有子级,则返回空字符串。
如果当前节点既不是元素也不是属性,则返回空字符串。
例外
XML 格式不正确,或者分析 XML 时出错。
在上一个异步操作完成之前调用了一个 XmlReader 方法。 在这种情况下, InvalidOperationException 会引发消息“正在进行异步操作”。
示例
下面的示例比较了 ReadInnerXml 方法和 ReadOuterXml 方法。
// Load the file and ignore all white space.
XmlReaderSettings settings = new XmlReaderSettings();
settings.IgnoreWhitespace = true;
using (XmlReader reader = XmlReader.Create("2books.xml")) {
// Moves the reader to the root element.
reader.MoveToContent();
// Moves to book node.
reader.Read();
// Note that ReadInnerXml only returns the markup of the node's children
// so the book's attributes are not returned.
Console.WriteLine("Read the first book using ReadInnerXml...");
Console.WriteLine(reader.ReadInnerXml());
// ReadOuterXml returns the markup for the current node and its children
// so the book's attributes are also returned.
Console.WriteLine("Read the second book using ReadOuterXml...");
Console.WriteLine(reader.ReadOuterXml());
}
' Load the file and ignore all white space.
Dim settings As New XmlReaderSettings()
settings.IgnoreWhitespace = True
Using reader As XmlReader = XmlReader.Create("2books.xml")
' Moves the reader to the root element.
reader.MoveToContent()
' Moves to book node.
reader.Read()
' Note that ReadInnerXml only returns the markup of the node's children
' so the book's attributes are not returned.
Console.WriteLine("Read the first book using ReadInnerXml...")
Console.WriteLine(reader.ReadInnerXml())
' ReadOuterXml returns the markup for the current node and its children
' so the book's attributes are also returned.
Console.WriteLine("Read the second book using ReadOuterXml...")
Console.WriteLine(reader.ReadOuterXml())
End Using
该示例使用 2books.xml 文件作为输入。
<!--sample XML fragment-->
<bookstore>
<book genre='novel' ISBN='10-861003-324'>
<title>The Handmaid's Tale</title>
<price>19.95</price>
</book>
<book genre='novel' ISBN='1-861001-57-5'>
<title>Pride And Prejudice</title>
<price>24.95</price>
</book>
</bookstore>
注解
此方法返回当前节点的所有内容,包括标记。 不会返回当前节点(开始标记)和相应的结束节点(结束标记)。 例如,如果有以下各项:
<node>
this <child id="123"/>
</node>
ReadInnerXml 返回 this <child id="123"/>
此方法通过以下方式处理元素和属性节点:
| 节点类型 | 呼叫前的位置 | XML 片段 | 返回值 | 呼叫后的位置 |
|---|---|---|---|---|
Element |
在 item1 开始标记上。 |
<item1>text1</item1><item2>text2</item2> | text1 | 在 item2 开始标记上。 |
Attribute |
在 attr1 属性节点上。 |
<item attr1=“val1” attr2=“val2”>text</item> | val1 |
attr1保留在属性节点上。 |
如果读取器定位在叶节点上,则调用 ReadInnerXml 等效于调用 Read。 该方法返回 String.Empty (属性节点除外),在这种情况下返回属性的值。
此方法检查格式正确的 XML。 如果 ReadInnerXml 从某个 XmlValidatingReader方法调用,此方法还会验证返回的内容。
如在中实现,XmlNodeReaderXmlTextReader并且XmlValidatingReader该方法的类ReadOuterXml可以识别命名空间。
有关此方法的异步版本,请参阅 ReadInnerXmlAsync。