XmlReader.ReadOuterXml 메서드

정의

파생 클래스에서 재정의되면 태그를 포함하여 이 노드 및 모든 자식 노드를 나타내는 콘텐츠를 읽습니다.

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

반환

판독기가 Element 또는 Attribute 노드에 배치되면 이 메서드는 태그를 포함해 현재 노드와 모든 자식 노드의 xml 콘텐츠를 모두 반환하고, 그렇지 않으면 빈 문자열을 반환합니다.

예외

XML의 형식이 잘못되었거나 XML을 구문 분석하는 동안 오류가 발생한 경우

이전 비동기 작업이 완료되기 전에 XmlReader 메서드가 호출되었습니다. 이 경우 “비동기 작업이 이미 진행 중입니다.” 메시지를 나타내며 InvalidOperationException이 throw됩니다.

예제

다음 예제에서는 및 ReadOuterXml 메서드를 ReadInnerXml 비교합니다.

// 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 남아 있습니다.

판독기가 리프 노드에 있는 경우 호출은 를 호출 ReadOuterXmlRead하는 것과 같습니다. 메서드가 반환됩니다 String.Empty (특성 노드를 제외하고 특성 태그가 반환됨).

이 메서드는 올바른 형식의 XML을 확인합니다. 가 에서 XmlValidatingReader호출되는 경우 ReadOuterXml 이 메서드는 반환된 콘텐츠의 유효성도 검사합니다.

XmlTextReaderXmlValidatingReader 클래스에서 XmlNodeReader구현된 대로 메서드는 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.

적용 대상