XmlTextWriter의 특성 네임스페이스 접두사

업데이트: November 2007

XmlTextWriterWriteAttributes, WriteAttributeStringWriteStartAttribute를 사용하여 여러 가지 방법으로 특성에 대한 네임스페이스 접두사를 처리할 수 있습니다.

WriteAttributes

현재 위치가 요소 노드이면 WriteAttributes 메서드가 XmlReader의 현재 위치에서 찾은 모든 특성을 작성합니다. 현재 위치가 특성에 설정되어 있으면 해당 특성과 요소에 남아 있는 모든 특성이 반환됩니다. 위치가 XmlDeclaration NodeType에 설정되어 있으면 선언의 모든 특성이 작성됩니다. 다른 모든 NodeType은 동작을 수행하지 않습니다. XmlReader에 선언된 네임스페이스 접두사가 있으면 WriteAttributes 메서드가 특성과 함께 네임스페이스 접두사를 작성합니다. 다음 코드 예제에서는 WriteAttributes 메서드가 특성과 함께 네임스페이스 접두사를 작성하는 방법을 보여 줍니다.

Dim r As XmlTextReader = CreateTextReaderStr("<ROOT xmlns:p='n' p:a='abc'/>")
r.Read()
r.MoveToAttribute("p:a")

Dim tw As New XmlTextWriter(Console.Out)
tw.WriteStartElement("ROOT")
tw.WriteAttributes(r, False)
tw.WriteEndElement()
XmlTextReader r = CreateTextReaderStr("<ROOT xmlns:p='n' p:a='abc'/>");
r.Read();
r.MoveToAttribute("p:a");

XmlTextWriter tw = new XmlTextWriter(Console.Out);
tw.WriteStartElement("ROOT");
tw.WriteAttributes(r, false);
tw.WriteEndElement();

출력

<ROOT p:a="abc" xmlns:p="n" />

WriteAttributeString

다른 방법으로 네임스페이스의 접두사를 처리하려면 응용 프로그램에서 네임스페이스를 접두사로 사용하는 WriteAttributeString 메서드 중 하나를 사용하도록 합니다. WriteAttributeString 메서드 중 하나가 특성 이름, 특성 값, 네임스페이스를 인수로 사용하여, 지정된 값을 가진 특성을 작성한 다음 네임스페이스와 연결합니다.

다른 WriteAttributeString 메서드는 사용자 정의 네임스페이스 접두사를 사용하여 특성을 작성할 때 특성을 이 접두사와 연결합니다. WriteAttributeString 메서드와 사용자 정의 네임스페이스 접두사 사용 방법을 보여 주는 샘플 코드는 WriteAttributeString을 참조하십시오.

WriteStartAttribute

WriteStartAttribute는 특성의 시작을 생성할 뿐만 아니라 사용한 메서드에 따라 네임스페이스 접두사나 네임스페이스 URI(Uniform Resource Identifier)를 매개 변수로 사용합니다.

다음 코드 예제에서는 첫 번째 입력 인수가 문자열로서 LookupPrefix 메서드를 호출하여 찾은 네임스페이스 접두사 bk를 사용하는 방법을 보여 줍니다. WriteStartAttribute를 사용하여 접두사, 로컬 이름 및 네임스페이스를 설정하면 WriteString 메서드가 특성에 값을 지정합니다.

' Write an element (this one is the root).
writer.WriteStartElement("bookstore")
' Write the namespace declaration.
writer.WriteAttributeString("xmlns", "bk", Nothing, "urn:samples")
writer.WriteStartElement("book")
' Look up the prefix, and then write the ISBN attribute.
Dim prefix As String = writer.LookupPrefix("urn:samples")
writer.WriteStartAttribute(prefix, "ISBN", "urn:samples")
writer.WriteString("1-861003-78")
writer.WriteEndAttribute()
' Write the style element.
writer.WriteStartElement(prefix, "style", "urn:samples")
writer.WriteString("hardcover")
writer.WriteEndElement()
' Write the end tag for the book element.
writer.WriteEndElement()
'Write the close tag for the root element.
writer.WriteEndElement()
                
// Write an element (this one is the root).
writer.WriteStartElement("bookstore");   
// Write the namespace declaration.
writer.WriteAttributeString("xmlns", "bk", null, "urn:samples");
writer.WriteStartElement("book");
// Look up the prefix, and then write the ISBN attribute.
string prefix = writer.LookupPrefix("urn:samples");
writer.WriteStartAttribute(prefix, "ISBN", "urn:samples");
writer.WriteString("1-861003-78");
writer.WriteEndAttribute();
// Write the style element.
writer.WriteStartElement(prefix, "style", "urn:samples");
writer.WriteString("hardcover");
writer.WriteEndElement();
// Write the end tag for the book element.
writer.WriteEndElement();
// Write the close tag for the root element.
writer.WriteEndElement();

출력

<bookstore xmlns:bk="urn:samples">
  <book bk:ISBN="1-861003-78">
      <bk:style>hardcover</bk:style>
  </book>
</bookstore>

특성에 연결된 네임스페이스 URI가 있으면 XML 권장 사항의 W3C(World Wide Web 컨소시엄) 네임스페이스에 있는 네임스페이스 기본 설정에 따라 특성에도 접두사가 있어야 합니다. 다음 코드 예제에서는 WriteAttributeString 메서드를 사용하여 특성을 작성할 때는 접두사도 포함해야 함을 보여 줍니다.

Dim w As New XmlTextWriter(Console.Out)
w.WriteStartElement("root")
w.WriteAttributeString("order", "urn:1", "123")
w.WriteEndElement()
w.Close()
XmlTextWriter w = new XmlTextWriter(Console.Out);
w.WriteStartElement("root");
w.WriteAttributeString("order","urn:1", "123");
w.WriteEndElement();
w.Close();

출력

<root n1:order="123" xmlns:n1="urn:1"/>

root 요소가 기본 네임스페이스인 urn:1과 연결되어 있을 경우에도 위와 같이 출력됩니다. 접두사의 이름은 *n{i}*이며, 여기서 i는 1부터 시작합니다. 또한 인덱스는 각 요소에 대해 다시 1부터 시작합니다. 따라서 중첩된 자식 요소에 생성된 접두사가 필요한 경우에는 n1을 사용합니다.

다음 코드 예제에서는 네임스페이스가 서로 다른 특성을 여러 개 작성할 때는 특성이 네임스페이스 선언 앞에 작성됨을 보여 줍니다.

Dim w As New XmlTextWriter(Console.Out)
w.WriteStartElement("root")
w.WriteAttributeString("order", "urn:1", "123")
w.WriteAttributeString("book", "urn:2", "The Great Escape")
w.WriteEndElement()
w.Close()
XmlTextWriter w = new XmlTextWriter(Console.Out);
w.WriteStartElement("root");
w.WriteAttributeString("order","urn:1", "123");
w.WriteAttributeString("book","urn:2", "The Great Escape");
w.WriteEndElement();
w.Close();

출력

<root n1:order="123" n2:book="The Great Escape" xmlns:n1="urn:1" xmlns:n2="urn:2"/>

참고 항목

참조

XmlTextWriter

XmlTextWriter

XmlWriter

XmlWriter