다음을 통해 공유


XDocument 클래스 개요

이 항목에서는 XDocument 클래스에 대해 소개합니다.

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 개체(수에 제한 없음). 주석은 루트 요소의 형제입니다. XML 문서는 주석으로 시작될 수 없으므로 XComment 개체는 목록의 첫 번째 인수일 수 없습니다.

  • DTD에 대한 XDocumentType 하나

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

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

XDocument 없이 XElement 사용

앞에서 설명했듯이 XElement 클래스는 LINQ to XML 프로그래밍 인터페이스의 기본 클래스입니다. 응용 프로그램에서는 대부분의 경우 문서를 만들도록 요구하지 않습니다. XElement 클래스를 사용하여 XML 트리를 만들고, 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.-->

참고 항목

개념

LINQ to XML 프로그래밍 개요