XmlReader.ReadOuterXml 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
在派生类中重写时,读取内容,包括标记,表示此节点及其所有子级。
public:
virtual System::String ^ ReadOuterXml();
public virtual string ReadOuterXml();
abstract member ReadOuterXml : unit -> string
override this.ReadOuterXml : unit -> string
Public Overridable Function ReadOuterXml () 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>
注解
此方法与 ReadInnerXml 此方法类似,但它也返回开始和结束标记。
此方法通过以下方式处理元素和属性节点:
| 节点类型 | 呼叫前的位置 | XML 片段 | 返回值 | 呼叫后的位置 |
|---|---|---|---|---|
Element |
在 item1 开始标记上。 |
<item1>text1</item1><item2>text2</item2> | <item1>text1</item1> | 在 item2 开始标记上。 |
Attribute |
在 attr1 属性节点上。 |
<item attr1=“val1” attr2=“val2”>text</item> | attr1=“val1” |
attr1保留在属性节点上。 |
如果读取器定位在叶节点上,则调用 ReadOuterXml 等效于调用 Read。 该方法返回 String.Empty (属性节点除外),在这种情况下返回属性标记。
此方法检查格式正确的 XML。 如果 ReadOuterXml 从某个 XmlValidatingReader方法调用,此方法还会验证返回的内容
如在中实现,XmlNodeReaderXmlTextReader并且XmlValidatingReader该方法的类ReadOuterXml可以识别命名空间。 给定以下 XML 文本 <A xmlns:S="urn:1"><S:B>hello</S:B></A>,如果读取器定位在起始标记上 S:B , ReadOuterXml 则 <S:B xmlns:S="urn:1">hello<S:B/>返回。
有关此方法的异步版本,请参阅 ReadOuterXmlAsync。