XmlWriter.WriteNode 메서드

정의

소스 개체 전체를 현재 작성기 인스턴스에 복사합니다.

오버로드

WriteNode(XmlReader, Boolean)

파생 클래스에서 재정의되면 판독기에서 작성기로 모든 항목을 복사하고 판독기를 다음 형제 노드의 시작 부분으로 이동합니다.

WriteNode(XPathNavigator, Boolean)

XPathNavigator 개체 전체를 작성기에 복사합니다. XPathNavigator 위치는 변경되지 않습니다.

설명

이 메서드의 비동기 버전은 다음을 참조하세요 WriteNodeAsync.

WriteNode(XmlReader, Boolean)

파생 클래스에서 재정의되면 판독기에서 작성기로 모든 항목을 복사하고 판독기를 다음 형제 노드의 시작 부분으로 이동합니다.

public:
 virtual void WriteNode(System::Xml::XmlReader ^ reader, bool defattr);
public virtual void WriteNode (System.Xml.XmlReader reader, bool defattr);
abstract member WriteNode : System.Xml.XmlReader * bool -> unit
override this.WriteNode : System.Xml.XmlReader * bool -> unit
Public Overridable Sub WriteNode (reader As XmlReader, defattr As Boolean)

매개 변수

reader
XmlReader

읽을 소스 XmlReader입니다.

defattr
Boolean

true에서 기본 특성을 복사하려면 XmlReader이고, 그렇지 않으면 false입니다.

예외

reader이(가) null인 경우

reader에 잘못된 문자가 들어 있는 경우

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

예제

다음 예제에서는 첫 번째 및 마지막 책 노드를 콘솔에 씁니다.

#using <System.Xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
int main()
{
   XmlTextReader^ reader = gcnew XmlTextReader( "books.xml" );
   reader->WhitespaceHandling = WhitespaceHandling::None;
   
   // Move the reader to the first book element.
   reader->MoveToContent();
   reader->Read();
   
   // Create a writer that outputs to the console.
   XmlTextWriter^ writer = gcnew XmlTextWriter( Console::Out );
   writer->Formatting = Formatting::Indented;
   
   // Write the start tag.
   writer->WriteStartElement( "myBooks" );
   
   // Write the first book.
   writer->WriteNode( reader, false );
   
   // Skip the second book.
   reader->Skip();
   
   // Write the last book.
   writer->WriteNode( reader, false );
   writer->WriteEndElement();
   
   // Close the writer and the reader.
   writer->Close();
   reader->Close();
}
using System;
using System.IO;
using System.Xml;

public class Sample{

  public static void Main(){

    XmlTextReader reader = new XmlTextReader("books.xml");
    reader.WhitespaceHandling = WhitespaceHandling.None;

    //Move the reader to the first book element.
    reader.MoveToContent();
    reader.Read();

    //Create a writer that outputs to the console.
    XmlTextWriter writer = new XmlTextWriter (Console.Out);
    writer.Formatting = Formatting.Indented;
    
    //Write the start tag.
    writer.WriteStartElement("myBooks");

    //Write the first book.
    writer.WriteNode(reader, false);

    //Skip the second book.
    reader.Skip();

    //Write the last book.
    writer.WriteNode(reader, false);
    writer.WriteEndElement();

    //Close the writer and the reader.
    writer.Close();
    reader.Close();
  }
}
Imports System.IO
Imports System.Xml

public class Sample

  public shared sub Main()

    Dim reader as XmlTextReader = new XmlTextReader("books.xml")
    reader.WhitespaceHandling = WhitespaceHandling.None

    'Move the reader to the first book element.
    reader.MoveToContent()
    reader.Read()

    'Create a writer that outputs to the console.
    Dim writer as XmlTextWriter = new XmlTextWriter (Console.Out)
    writer.Formatting = Formatting.Indented
    
    'Write the start tag.
    writer.WriteStartElement("myBooks")

    'Write the first book.
    writer.WriteNode(reader, false)

    'Skip the second book.
    reader.Skip()

    'Write the last book.
    writer.WriteNode(reader, false)
    writer.WriteEndElement()

    'Close the writer and the reader.
    writer.Close()
    reader.Close()

  end sub
end class

이 예제에서는 파일을 books.xml입력으로 사용합니다.

<bookstore>
  <book genre="autobiography" publicationdate="1981" ISBN="1-861003-11-0">
    <title>The Autobiography of Benjamin Franklin</title>
    <author>
      <first-name>Benjamin</first-name>
      <last-name>Franklin</last-name>
    </author>
    <price>8.99</price>
  </book>
  <book genre="novel" publicationdate="1967" ISBN="0-201-63361-2">
    <title>The Confidence Man</title>
    <author>
      <first-name>Herman</first-name>
      <last-name>Melville</last-name>
    </author>
    <price>11.99</price>
  </book>
  <book genre="philosophy" publicationdate="1991" ISBN="1-861001-57-6">
    <title>The Gorgias</title>
    <author>
      <name>Plato</name>
    </author>
    <price>9.99</price>
  </book>
</bookstore>

설명

다음 표에서는 이 메서드에 대해 지원되는 노드 형식을 보여줍니다.

노드 형식 WriteNode 동작
None 형식에 관계없이 모든 노드를 씁니다. 즉, 작성기는 특성, 처리 지침, 주석 등을 포함하여 읽은 모든 노드를 사용하고 XmlReader 씁니다.

이 상황은 초기 상태일 때 XmlReader 발생합니다. (속성이 XmlReader.ReadState 반환 ReaderState.Initial됨).
Element 요소 노드 및 모든 특성 노드를 씁니다.
Attribute 작동하지 않습니다. 대신 WriteStartAttribute 또는 WriteAttributeString를 사용하십시오.
Text 텍스트 노드를 씁니다.
CDATA CDATA 섹션 노드를 씁니다.
EntityReference 엔터티 참조 노드를 씁니다.
ProcessingInstruction 처리 명령 노드를 씁니다.
Comment 주석 노드를 씁니다.
DocumentType 문서 형식 노드를 씁니다.
SignificantWhitespace 중요한 공백 노드를 씁니다.
Whitespace 공백 노드를 씁니다.
EndElement 끝 요소 태그를 씁니다.
EndEntity 작동하지 않습니다.
XmlDeclaration XML 선언 노드를 씁니다.

판독기 초기 상태이면 이 메서드는 판독기를 파일의 끝으로 이동합니다. 판독기는 이미 파일의 끝에 있거나 닫힌 상태인 경우 이 메서드는 작동되지 않습니다.

다음 C# 코드는 전체 XML 입력 문서를 콘솔에 복사합니다.

XmlReader reader = XmlReader.Create(myfile);  
XmlWriter writer = XmlWriter.Create(Console.Out);  
writer.WriteNode(reader, false);  

루트 노드를 벗어나 문서의 다른 위치에 배치된 경우 다음 C# 예제에서는 노드를 올바르게 작성합니다.

XmlReader reader = XmlReader.Create(myfile);  
reader.Read(); // Read PI  
reader.Read(); // Read Comment  
reader.Read(); // Read DOCType  
XmlWriter writer = XmlWriter.Create(Console.Out);  
while (!reader.EOF){  
  writer.WriteNode(reader, false);  
 }  

판독기가 공백을 반환하도록 구성되어 있고 기록기가 출력 WriteNode 을 들여쓰도록 구성된 경우 이상한 출력이 생성될 수 있습니다. 기본적으로 이중 서식이 지정됩니다.

이 메서드의 비동기 버전은 다음을 참조하세요 WriteNodeAsync.

적용 대상

WriteNode(XPathNavigator, Boolean)

XPathNavigator 개체 전체를 작성기에 복사합니다. XPathNavigator 위치는 변경되지 않습니다.

public:
 virtual void WriteNode(System::Xml::XPath::XPathNavigator ^ navigator, bool defattr);
public virtual void WriteNode (System.Xml.XPath.XPathNavigator navigator, bool defattr);
abstract member WriteNode : System.Xml.XPath.XPathNavigator * bool -> unit
override this.WriteNode : System.Xml.XPath.XPathNavigator * bool -> unit
Public Overridable Sub WriteNode (navigator As XPathNavigator, defattr As Boolean)

매개 변수

navigator
XPathNavigator

복사할 원본 XPathNavigator입니다.

defattr
Boolean

기본 특성을 복사하려면 true이고, 그렇지 않으면 false입니다.

예외

navigator이(가) null인 경우

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

예제

다음 예제에서는 메서드를 WriteNode 사용하여 문서에서 첫 번째 책 노드를 복사하여 콘솔에 씁니다.

using System;
using System.IO;
using System.Xml;
using System.Xml.XPath;

public class Sample
{

    public static void Main()
    {

        XPathDocument doc = new XPathDocument("books.xml");
        XPathNavigator nav = doc.CreateNavigator();

        // Create a writer that outputs to the console.
        XmlWriter writer = XmlWriter.Create(Console.Out);

        // Write the start tag.
        writer.WriteStartElement("myBooks");

        // Write the first book.
        nav.MoveToChild("bookstore", "");
        nav.MoveToChild("book", "");
        writer.WriteNode(nav, false);

        // Close the start tag.
        writer.WriteEndElement();

        // Close the writer.
        writer.Close();
    }
}
Imports System.IO
Imports System.Xml
Imports System.Xml.XPath

Module Module1

    Sub Main()

        Dim doc As XPathDocument = New XPathDocument("books.xml")
        Dim nav As XPathNavigator = doc.CreateNavigator()

        ' Create a writer that outputs to the console.
        Dim writer As XmlWriter = XmlWriter.Create(Console.Out)

        ' Write the start tag.
        writer.WriteStartElement("myBooks")

        ' Write the first book.
        nav.MoveToChild("bookstore", "")
        nav.MoveToChild("book", "")
        writer.WriteNode(nav, False)

        ' Close the start tag.
        writer.WriteEndElement()

        ' Close the writer.
        writer.Close()

    End Sub
End Module

이 예제에서는 books.xml 파일을 입력으로 사용합니다.

<?xml version="1.0" encoding="utf-8" ?> 
<bookstore>
    <book genre="autobiography" publicationdate="1981-03-22" ISBN="1-861003-11-0">
        <title>The Autobiography of Benjamin Franklin</title>
        <author>
            <first-name>Benjamin</first-name>
            <last-name>Franklin</last-name>
        </author>
        <price>8.99</price>
    </book>
    <book genre="novel" publicationdate="1967-11-17" ISBN="0-201-63361-2">
        <title>The Confidence Man</title>
        <author>
            <first-name>Herman</first-name>
            <last-name>Melville</last-name>
        </author>
        <price>11.99</price>
    </book>
    <book genre="philosophy" publicationdate="1991-02-15" ISBN="1-861001-57-6">
        <title>The Gorgias</title>
        <author>
            <name>Plato</name>
        </author>
        <price>9.99</price>
    </book>
</bookstore>

설명

다음 표에서는 이 메서드에 대해 지원되는 XPath 노드 형식을 보여줍니다.

XPathNodeType WriteNode 동작
Root 형식에 관계없이 모든 노드를 씁니다. 즉, 작성기는 루트 노드의 모든 노드(특성, 처리 지침, 주석 등)를 사용하고 XPathNavigator 기록합니다.
Element 요소 노드 및 모든 특성 노드를 씁니다.
Attribute 작동하지 않습니다. 대신 WriteStartAttribute 또는 WriteAttributeString를 사용하십시오.
Text 텍스트 노드를 씁니다.
Namespace 작동하지 않습니다. 또는 WriteAttributeString 메서드를 WriteStartAttribute 사용하여 네임스페이스 선언을 작성합니다.
ProcessingInstruction 처리 명령 노드를 씁니다.
Comment 주석 노드를 씁니다.
SignificantWhitespace 중요한 공백 노드를 씁니다.
Whitespace 공백 노드를 씁니다.

이 메서드의 비동기 버전은 다음을 참조하세요 WriteNodeAsync.

적용 대상