C#에서 네임스페이스를 사용하여 문서를 만드는 방법(LINQ to XML)
이 문서에서는 네임스페이스가 있는 C#에서 문서를 만드는 방법을 보여 줍니다.
예: 기본 네임스페이스 선언 및 초기화
네임스페이스에 있는 요소 또는 특성을 만들려면 먼저 XNamespace 개체를 선언하고 초기화합니다. 그런 다음 더하기 연산자 오버로드를 사용하여 네임스페이스를 문자열로 표현된 로컬 이름과 결합합니다.
다음 예제에서는 네임스페이스가 하나 있는 문서를 만듭니다. 기본적으로 LINQ to XML은 이 문서를 기본 네임스페이스로 직렬화합니다.
// Create an XML tree in a namespace.
XNamespace aw = "http://www.adventure-works.com";
XElement root = new XElement(aw + "Root",
new XElement(aw + "Child", "child content")
);
Console.WriteLine(root);
이 예제에서는 다음 출력을 생성합니다.
<Root xmlns="http://www.adventure-works.com">
<Child>child content</Child>
</Root>
예: 네임스페이스와 특성이 있는 문서 만들기
다음 예제에서는 네임스페이스가 하나 있는 문서를 만듭니다. 또한 네임스페이스 접두사를 사용하여 네임스페이스를 선언하는 특성을 만듭니다. 접두사를 사용하여 네임스페이스를 선언하는 특성을 만들려면 특성 이름이 네임스페이스 접두사이고 이 이름이 Xmlns 네임스페이스에 있는 특성을 만듭니다. 이 특성의 값은 네임스페이스의 URI입니다.
// Create an XML tree in a namespace, with a specified prefix
XNamespace aw = "http://www.adventure-works.com";
XElement root = new XElement(aw + "Root",
new XAttribute(XNamespace.Xmlns + "aw", "http://www.adventure-works.com"),
new XElement(aw + "Child", "child content")
);
Console.WriteLine(root);
이 예제에서는 다음 출력을 생성합니다.
<aw:Root xmlns:aw="http://www.adventure-works.com">
<aw:Child>child content</aw:Child>
</aw:Root>
예: 접두사가 있는 네임스페이스 하나를 포함하여 네임스페이스가 두 개 있는 문서 만들기
다음 예제에서는 두 네임스페이스를 포함하는 문서를 만드는 방법을 보여 줍니다. 하나는 기본 네임스페이스이고, 다른 하나는 접두사를 가진 네임스페이스입니다.
루트 요소에 네임스페이스 특성을 포함하면 http://www.adventure-works.com
은 기본 네임스페이스로 직렬화되고, www.fourthcoffee.com
은 fc
접두사가 붙여져 직렬화됩니다. 기본 네임스페이스를 선언하는 특성을 만들려면 네임스페이스 없이 이름이 xmlns
특성을 만듭니다. 특성 값은 기본 네임스페이스 URI입니다.
기본 네임스페이스 선언이 범위에 있는 경우, 로컬 이름에 해당 XNamespace
개체를 접두사로 지정하여 자식 XElement
개체에 적용됩니다. 반면에 기본 네임스페이스 선언은 특성 이름에 직접 적용되지 않습니다. 따라서 기본 네임스페이스의 XAttribute
개체는 해당 XNamespace
개체를 접두사로 정의되지 않습니다.
// The http://www.adventure-works.com namespace is forced to be the default namespace.
XNamespace aw = "http://www.adventure-works.com";
XNamespace fc = "www.fourthcoffee.com";
XElement root = new XElement(aw + "Root",
new XAttribute("xmlns", "http://www.adventure-works.com"),
new XAttribute(XNamespace.Xmlns + "fc", "www.fourthcoffee.com"),
new XElement(fc + "Child",
new XElement(aw + "DifferentChild", "other content")
),
new XElement(aw + "Child2", "c2 content",
new XAttribute("DefaultNs", "default namespace"),
new XAttribute(fc + "PrefixedNs", "prefixed namespace")
),
new XElement(fc + "Child3", "c3 content",
new XAttribute("DefaultNs", "default namespace"),
new XAttribute(fc + "PrefixedNs", "prefixed namespace")
)
);
Console.WriteLine(root);
이 예제에서는 다음 출력을 생성합니다.
<Root xmlns="http://www.adventure-works.com" xmlns:fc="www.fourthcoffee.com">
<fc:Child>
<DifferentChild>other content</DifferentChild>
</fc:Child>
<Child2 DefaultNs="default namespace" fc:PrefixedNs="prefixed namespace">c2 content</Child2>
<fc:Child3 DefaultNs="default namespace" fc:PrefixedNs="prefixed namespace">c3 content</fc:Child3>
</Root>
예: 두 개의 네임스페이스가 있는 문서 만들기(둘 다 접두사 사용)
다음 예제에서는 네임스페이스 접두사를 사용하는 두 네임스페이스가 포함된 문서를 만듭니다.
XNamespace aw = "http://www.adventure-works.com";
XNamespace fc = "www.fourthcoffee.com";
XElement root = new XElement(aw + "Root",
new XAttribute(XNamespace.Xmlns + "aw", aw.NamespaceName),
new XAttribute(XNamespace.Xmlns + "fc", fc.NamespaceName),
new XElement(fc + "Child",
new XElement(aw + "DifferentChild", "other content")
),
new XElement(aw + "Child2", "c2 content"),
new XElement(fc + "Child3", "c3 content")
);
Console.WriteLine(root);
이 예제에서는 다음 출력을 생성합니다.
<aw:Root xmlns:aw="http://www.adventure-works.com" xmlns:fc="www.fourthcoffee.com">
<fc:Child>
<aw:DifferentChild>other content</aw:DifferentChild>
</fc:Child>
<aw:Child2>c2 content</aw:Child2>
<fc:Child3>c3 content</fc:Child3>
</aw:Root>
예: 확장된 이름을 사용하여 네임스페이스 만들기
동일한 결과를 수행하는 또 다른 방법은 XNamespace 개체를 선언하고 만드는 대신 확장된 이름을 사용하는 것입니다.
이 방법은 성능에 영향을 줍니다. 확장된 이름을 포함하는 문자열을 LINQ to XML에 전달할 때마다 LINQ to XML은 이름을 구문 분석하고 원자화된 네임스페이스를 찾고 원자화된 이름을 찾아야 합니다. 이 프로세스는 CPU 시간이 걸립니다. 성능이 중요한 경우 XNamespace 개체를 명시적으로 선언하고 사용하는 것이 좋습니다.
성능이 중요한 문제인 경우 자세한 내용은 XName 개체 사전 원자화를 참조하세요.
// Create an XML tree in a namespace, with a specified prefix
XElement root = new XElement("{http://www.adventure-works.com}Root",
new XAttribute(XNamespace.Xmlns + "aw", "http://www.adventure-works.com"),
new XElement("{http://www.adventure-works.com}Child", "child content")
);
Console.WriteLine(root);
이 예제에서는 다음 출력을 생성합니다.
<aw:Root xmlns:aw="http://www.adventure-works.com">
<aw:Child>child content</aw:Child>
</aw:Root>
참고 항목
.NET