XDocument 클래스 개요

XDocument 클래스에는 XML 선언, 처리 명령 및 주석을 포함하여 유효한 XML 문서에 필요한 정보가 포함되어 있습니다.

XDocument 클래스에서 제공하는 특정 기능이 필요한 경우 XDocument 개체만 만들면 됩니다. 대부분의 경우 XElement로 직접 작업할 수 있습니다. XElement로 직접 작업하는 것이 더 간단한 프로그래밍 모델입니다.

XDocumentXContainer에서 파생되므로 자식 노드를 포함할 수 있습니다. 그러나 XDocument 개체에는 자식 XElement 노드가 하나만 포함될 수 있습니다. 이는 XML 문서에 루트 요소가 하나만 있어야 하는 XML 표준을 반영하는 것입니다.

XDocument의 구성 요소

XDocument에는 다음과 같은 요소가 포함될 수 있습니다.

  • XDeclaration 개체 하나. XDeclaration을 통해 XML 선언의 관련 부분인 XML 버전, 문서의 인코딩 및 XML 문서가 독립 실행형인지 여부를 지정할 수 있습니다.
  • XElement 개체 하나. 이 개체는 XML 문서의 루트 노드입니다.
  • XProcessingInstruction 개체(수에 제한 없음). 처리 명령은 XML을 처리하는 정보를 애플리케이션에 전달합니다.
  • XComment 개체(수에 제한 없음). 주석은 루트 요소의 형제입니다.
  • DTD에 대한 XDocumentType 하나

XDocument를 serialize할 때 XDocument.Declarationnull인 경우에도 작성기의 Writer.Settings.OmitXmlDeclarationfalse(기본값)로 설정되어 있으면 출력에 XML 선언이 포함됩니다.

기본적으로 LINQ to XML은 버전을 "1.0"으로 설정하고 인코딩을 "utf-8"로 설정합니다.

XDocument 없이 XElement 사용

앞서 언급한 것처럼 XElement 클래스는 LINQ to XML 프로그래밍 인터페이스의 기본 클래스입니다. 대부분의 경우 애플리케이션에서는 문서를 만들 것을 요구하지 않습니다. XElement 클래스를 사용하면 다음을 수행할 수 있습니다.

  • XML 트리를 만듭니다.
  • 여기에 다른 XML 트리를 추가합니다.
  • XML 트리를 수정합니다.
  • 저장합니다.

XDocument 사용

XDocument를 생성하려면 XElement 개체를 생성할 때와 마찬가지로 함수 생성을 사용합니다.

다음 예에서는 XDocument 개체와 포함된 관련 개체를 만듭니다.

XDocument d = new XDocument(
    new XComment("This is a comment."),
    new XProcessingInstruction("xml-stylesheet",
        "href='mystyle.css' title='Compact' type='text/css'"),
    new XElement("Pubs",
        new XElement("Book",
            new XElement("Title", "Artifacts of Roman Civilization"),
            new XElement("Author", "Moreno, Jordao")
        ),
        new XElement("Book",
            new XElement("Title", "Midieval Tools and Implements"),
            new XElement("Author", "Gazit, Inbar")
        )
    ),
    new XComment("This is another comment.")
);
d.Declaration = new XDeclaration("1.0", "utf-8", "true");
Console.WriteLine(d);

d.Save("test.xml");
Dim doc As XDocument = <?xml version="1.0" encoding="utf-8"?>
                       <!--This is a comment.-->
                       <?xml-stylesheet href='mystyle.css' title='Compact' type='text/css'?>
                       <Pubs>
                           <Book>
                               <Title>Artifacts of Roman Civilization</Title>
                               <Author>Moreno, Jordao</Author>
                           </Book>
                           <Book>
                               <Title>Midieval Tools and Implements</Title>
                               <Author>Gazit, Inbar</Author>
                           </Book>
                       </Pubs>
                       <!--This is another comment.-->
doc.Save("test.xml")

이 예에서는 test.xml에 다음 출력을 생성합니다.

<?xml version="1.0" encoding="utf-8"?>
<!--This is a comment.-->
<?xml-stylesheet href='mystyle.css' title='Compact' type='text/css'?>
<Pubs>
  <Book>
    <Title>Artifacts of Roman Civilization</Title>
    <Author>Moreno, Jordao</Author>
  </Book>
  <Book>
    <Title>Midieval Tools and Implements</Title>
    <Author>Gazit, Inbar</Author>
  </Book>
</Pubs>
<!--This is another comment.-->

참고 항목