XDocument.Save 메서드

정의

이를 XDocument 파일, TextWritera 또는 XmlWriter.에 직렬화합니다.

오버로드

Name Description
Save(XmlWriter)

이를 XDocument .에 직렬화합니다 XmlWriter.

Save(Stream)

지정된 .에 출력 XDocument 합니다 Stream.

Save(TextWriter)

이를 XDocument .로 직렬화합니다 TextWriter.

Save(String)

기존 XDocument 파일이 있는 경우 이를 파일로 직렬화하여 덮어씁니다.

Save(Stream, SaveOptions)

XDocument 필요에 따라 서식 지정 동작을 지정하여 지정된 Stream값으로 출력합니다.

Save(TextWriter, SaveOptions)

필요에 따라 서식을 XDocument사용하지 않도록 설정하여 이를 TextWriter 직렬화합니다.

Save(String, SaveOptions)

필요에 따라 서식을 사용하지 않도록 설정하여 파일로 직렬화 XDocument 합니다.

Save(XmlWriter)

Source:
XDocument.cs
Source:
XDocument.cs
Source:
XDocument.cs
Source:
XDocument.cs
Source:
XDocument.cs

이를 XDocument .에 직렬화합니다 XmlWriter.

public:
 void Save(System::Xml::XmlWriter ^ writer);
public void Save(System.Xml.XmlWriter writer);
member this.Save : System.Xml.XmlWriter -> unit
Public Sub Save (writer As XmlWriter)

매개 변수

writer
XmlWriter

XmlWriter 기록 XDocument 될 A입니다.

예제

다음 예제에서는 에 저장하는 XDocument 방법을 보여 있습니다 XmlWriter.

StringBuilder sb = new StringBuilder();
XmlWriterSettings xws = new XmlWriterSettings();
xws.OmitXmlDeclaration = true;
xws.Indent = true;

using (XmlWriter xw = XmlWriter.Create(sb, xws)) {
    XDocument doc = new XDocument(
        new XElement("Child",
            new XElement("GrandChild", "some content")
        )
    );
    doc.Save(xw);
}

Console.WriteLine(sb.ToString());
Dim sb As StringBuilder = New StringBuilder()
Dim xws As XmlWriterSettings = New XmlWriterSettings()
xws.OmitXmlDeclaration = True
xws.Indent = True

Using xw = XmlWriter.Create(sb, xws)
    Dim doc As XDocument = New XDocument(<Child><GrandChild>some content</GrandChild></Child>)
    doc.Save(xw)

End Using

Console.WriteLine(sb.ToString())

이 예제는 다음과 같은 출력을 생성합니다.

<Child>
  <GrandChild>some content</GrandChild>
</Child>

추가 정보

적용 대상

Save(Stream)

Source:
XDocument.cs
Source:
XDocument.cs
Source:
XDocument.cs
Source:
XDocument.cs
Source:
XDocument.cs

지정된 .에 출력 XDocument 합니다 Stream.

public:
 void Save(System::IO::Stream ^ stream);
public void Save(System.IO.Stream stream);
member this.Save : System.IO.Stream -> unit
Public Sub Save (stream As Stream)

매개 변수

stream
Stream

출력 XDocument 할 스트림입니다.

설명

직렬화된 XML이 들여쓰기됩니다. 모든 중요하지 않은 공백이 제거되고 XML이 제대로 들여쓰기되도록 추가 공백이 추가됩니다. 이 메서드의 동작은 중요하지 않은 공백이 유지되지 않는다는 것입니다.

공백을 제어하려면 매개 변수로 사용하는 Save 오버로드 SaveOptions 를 사용합니다. 이 DisableFormatting 옵션을 사용하여 인덱싱되지 않은 XML을 저장합니다. 이렇게 하면 작성기가 XML 트리에 표시된 것과 정확하게 모든 공백을 작성하게 됩니다.

중복 네임스페이스 선언을 제거하려면 옵션을 사용합니다 OmitDuplicateNamespaces .

적용 대상

Save(TextWriter)

Source:
XDocument.cs
Source:
XDocument.cs
Source:
XDocument.cs
Source:
XDocument.cs
Source:
XDocument.cs

이를 XDocument .로 직렬화합니다 TextWriter.

public:
 void Save(System::IO::TextWriter ^ textWriter);
public void Save(System.IO.TextWriter textWriter);
member this.Save : System.IO.TextWriter -> unit
Public Sub Save (textWriter As TextWriter)

매개 변수

textWriter
TextWriter

TextWriter 기록 XDocument 될 A입니다.

예제

다음 예제에서는 문서를 만들고 XDocument, 문서를 저장 StringWriter한 다음, 문자열을 콘솔에 인쇄합니다.

StringBuilder sb = new StringBuilder();

XDocument doc = new XDocument(
    new XElement("Root",
        new XElement("Child", "content")
    )
);
TextWriter tr = new StringWriter(sb);
doc.Save(tr);
Console.WriteLine(sb.ToString());
Dim sb As StringBuilder = New StringBuilder()

Dim doc As XDocument = _
    <?xml version="1.0" encoding="utf-8"?>
        <Root><Child>content</Child></Root>

Dim tr As TextWriter = New StringWriter(sb)
doc.Save(tr)
Console.WriteLine(sb.ToString())

이 예제는 다음과 같은 출력을 생성합니다.

<?xml version="1.0" encoding="utf-16"?>
<Root>
  <Child>content</Child>
</Root>

설명

직렬화된 XML이 들여쓰기됩니다. 모든 중요하지 않은 공백이 제거되고 XML이 제대로 들여쓰기되도록 추가 공백이 추가됩니다. 이 메서드의 동작은 중요하지 않은 공백이 유지되지 않는다는 것입니다.

공백을 제어하려면 매개 변수로 사용하는 Save 오버로드 SaveOptions 를 사용합니다. 자세한 내용은 XML을 로드하거나 구문 분석하는 동안 공백 유지를 참조하고 직렬화하는 동안 공백을 유지합니다.

추가 정보

적용 대상

Save(String)

Source:
XDocument.cs
Source:
XDocument.cs
Source:
XDocument.cs
Source:
XDocument.cs
Source:
XDocument.cs

기존 XDocument 파일이 있는 경우 이를 파일로 직렬화하여 덮어씁니다.

public:
 void Save(System::String ^ fileName);
public void Save(string fileName);
member this.Save : string -> unit
Public Sub Save (fileName As String)

매개 변수

fileName
String

파일 이름을 포함하는 문자열입니다.

예제

다음 예제에서는 XDocument문서를 만들고 파일에 저장한 다음 콘솔에 파일을 인쇄합니다.

XDocument doc = new XDocument(
    new XElement("Root",
        new XElement("Child", "content")
    )
);
doc.Save("Root.xml");
Console.WriteLine(File.ReadAllText("Root.xml"));
Dim doc As XDocument = _
    <?xml version="1.0" encoding="utf-8"?>
        <Root><Child>content</Child></Root>

doc.Save("Root.xml")
Console.WriteLine(File.ReadAllText("Root.xml"))

이 예제는 다음과 같은 출력을 생성합니다.

<?xml version="1.0" encoding="utf-8"?>
<Root>
  <Child>content</Child>
</Root>

설명

직렬화된 XML이 들여쓰기됩니다. 모든 중요하지 않은 공백이 제거되고 XML이 제대로 들여쓰기되도록 추가 공백이 추가됩니다. 이 메서드의 동작은 중요하지 않은 공백이 유지되지 않는다는 것입니다.

공백을 제어하려면 매개 변수로 사용하는 Save 오버로드 SaveOptions 를 사용합니다. 자세한 내용은 XML을 로드하거나 구문 분석하는 동안 공백 유지를 참조하고 직렬화하는 동안 공백을 유지합니다.

추가 정보

적용 대상

Save(Stream, SaveOptions)

Source:
XDocument.cs
Source:
XDocument.cs
Source:
XDocument.cs
Source:
XDocument.cs
Source:
XDocument.cs

XDocument 필요에 따라 서식 지정 동작을 지정하여 지정된 Stream값으로 출력합니다.

public:
 void Save(System::IO::Stream ^ stream, System::Xml::Linq::SaveOptions options);
public void Save(System.IO.Stream stream, System.Xml.Linq.SaveOptions options);
member this.Save : System.IO.Stream * System.Xml.Linq.SaveOptions -> unit
Public Sub Save (stream As Stream, options As SaveOptions)

매개 변수

stream
Stream

출력 XDocument 할 스트림입니다.

options
SaveOptions

서식 동작을 지정하는 A SaveOptions 입니다.

설명

기본적으로 이 설정 options 은 .로 None설정됩니다. 이 옵션은 불필요한 공백을 모두 제거하고 XML이 제대로 들여쓰기되도록 적절한 중요하지 않은 공백을 추가합니다.

인덱싱되지 않은 XML을 저장하려면 에 대한 DisableFormatting플래그를 options 지정합니다. 이렇게 하면 작성기가 XML 트리에 표시된 것과 정확하게 모든 공백을 작성하게 됩니다.

중복 네임스페이스 선언을 제거하려면 옵션을 사용합니다 OmitDuplicateNamespaces .

적용 대상

Save(TextWriter, SaveOptions)

Source:
XDocument.cs
Source:
XDocument.cs
Source:
XDocument.cs
Source:
XDocument.cs
Source:
XDocument.cs

필요에 따라 서식을 XDocument사용하지 않도록 설정하여 이를 TextWriter 직렬화합니다.

public:
 void Save(System::IO::TextWriter ^ textWriter, System::Xml::Linq::SaveOptions options);
public void Save(System.IO.TextWriter textWriter, System.Xml.Linq.SaveOptions options);
member this.Save : System.IO.TextWriter * System.Xml.Linq.SaveOptions -> unit
Public Sub Save (textWriter As TextWriter, options As SaveOptions)

매개 변수

textWriter
TextWriter

TextWriter XML을 출력할 형식입니다.

options
SaveOptions

서식 동작을 지정하는 A SaveOptions 입니다.

예제

다음 예제에서는 이 메서드의 두 가지 용도를 보여 있습니다. 첫 번째 사용에서는 서식을 XDocument 사용하여 직렬화합니다. 두 번째는 공백을 유지합니다. 문서에 생성된 공백이 없으므로 공백을 유지하면 XML이 어떤 내용도 출력되지 않습니다.

XDocument doc = new XDocument(
    new XElement("Root",
        new XElement("Child", "content")
    )
);
StringBuilder sb1 = new StringBuilder();
using (StringWriter sr1 = new StringWriter(sb1)) {
    doc.Save(sr1, SaveOptions.None);
    Console.WriteLine(sb1.ToString());
}

StringBuilder sb2 = new StringBuilder();
using (StringWriter sr2 = new StringWriter(sb2)) {
    doc.Save(sr2, SaveOptions.DisableFormatting);
    Console.WriteLine(sb2.ToString());
}
Dim doc As XDocument = _
    <?xml version="1.0" encoding="utf-8"?>
        <Root><Child>content</Child></Root>

Dim sb1 As StringBuilder = New StringBuilder()

Using sr1 = New StringWriter(sb1)
    doc.Save(sr1, SaveOptions.None)
    Console.WriteLine(sb1.ToString())
End Using

Dim sb2 As StringBuilder = New StringBuilder()

Using sr2 = New StringWriter(sb2)
    doc.Save(sr2, SaveOptions.DisableFormatting)
    Console.WriteLine(sb2.ToString())
End Using

이 예제는 다음과 같은 출력을 생성합니다.

<?xml version="1.0" encoding="utf-16"?>
<Root>
  <Child>content</Child>
</Root>
<?xml version="1.0" encoding="utf-16"?><Root><Child>content</Child></Root>

설명

인덱싱되지 않은 XML을 저장하려면 에 대한 DisableFormatting플래그를 options 지정합니다. 이렇게 하면 기록기가 XML 트리에 표시된 것과 정확히 같은 모든 공백을 작성하게 됩니다.

들여쓰기된 XML을 저장하려면 에 대한 DisableFormatting플래그를 options 지정하지 마세요. 이렇게 하면 불필요한 공백이 모두 제거되고 XML이 제대로 들여쓰기되도록 적절한 중요하지 않은 공백이 추가됩니다. 이는 기본 동작이며 매개 변수로 사용하지 Save 않는 메서드의 options 오버로드 동작입니다.

자세한 내용은 XML을 로드하거나 구문 분석하는 동안 공백 유지를 참조하고 직렬화하는 동안 공백을 유지합니다.

추가 정보

적용 대상

Save(String, SaveOptions)

Source:
XDocument.cs
Source:
XDocument.cs
Source:
XDocument.cs
Source:
XDocument.cs
Source:
XDocument.cs

필요에 따라 서식을 사용하지 않도록 설정하여 파일로 직렬화 XDocument 합니다.

public:
 void Save(System::String ^ fileName, System::Xml::Linq::SaveOptions options);
public void Save(string fileName, System.Xml.Linq.SaveOptions options);
member this.Save : string * System.Xml.Linq.SaveOptions -> unit
Public Sub Save (fileName As String, options As SaveOptions)

매개 변수

fileName
String

파일 이름을 포함하는 문자열입니다.

options
SaveOptions

서식 동작을 지정하는 A SaveOptions 입니다.

예제

다음 예제에서는 이 메서드의 두 가지 용도를 보여 있습니다. 첫 번째 사용은 공백을 유지합니다. 두 번째는 indenting을 XDocument 사용하여 직렬화합니다.

XDocument doc = new XDocument(
    new XElement("Root",
        new XElement("Child", "content")
    )
);
doc.Save("Root1.xml", SaveOptions.DisableFormatting);
Console.WriteLine(File.ReadAllText("Root1.xml"));
doc.Save("Root2.xml", SaveOptions.None);
Console.WriteLine(File.ReadAllText("Root2.xml"));
Dim doc As XDocument = _
    <?xml version="1.0" encoding="utf-8"?>
        <Root><Child>content</Child></Root>

doc.Save("Root1.xml", SaveOptions.DisableFormatting)
Console.WriteLine(File.ReadAllText("Root1.xml"))
doc.Save("Root2.xml", SaveOptions.None)
Console.WriteLine(File.ReadAllText("Root2.xml"))

이 예제는 다음과 같은 출력을 생성합니다.

<?xml version="1.0" encoding="utf-8"?><Root><Child>content</Child></Root>
<?xml version="1.0" encoding="utf-8"?>
<Root>
  <Child>content</Child>
</Root>

설명

인덱싱되지 않은 XML을 저장하려면 에 대한 DisableFormatting플래그를 options 지정합니다. 이렇게 하면 기록기가 XML 트리에 표시된 것과 정확히 같은 모든 공백을 작성하게 됩니다.

들여쓰기된 XML을 저장하려면 에 대한 DisableFormatting플래그를 options 지정하지 마세요. 이렇게 하면 불필요한 공백이 모두 제거되고 XML이 제대로 들여쓰기되도록 적절한 중요하지 않은 공백이 추가됩니다. 이는 기본 동작이며 매개 변수로 사용하지 Save 않는 메서드의 options 오버로드 동작입니다.

자세한 내용은 XML을 로드하거나 구문 분석하는 동안 공백 유지를 참조하고 직렬화하는 동안 공백을 유지합니다.

추가 정보

적용 대상