XmlNamespaceDeclarationsAttribute 클래스

정의

대상 속성, 매개 변수, 반환 값 또는 클래스 멤버에 XML 문서 내에서 사용되는 네임스페이스와 연결된 접두사를 포함되도록 지정합니다.

public ref class XmlNamespaceDeclarationsAttribute : Attribute
[System.AttributeUsage(System.AttributeTargets.Field | System.AttributeTargets.Parameter | System.AttributeTargets.Property | System.AttributeTargets.ReturnValue, AllowMultiple=false)]
public class XmlNamespaceDeclarationsAttribute : Attribute
[System.AttributeUsage(System.AttributeTargets.Field | System.AttributeTargets.Parameter | System.AttributeTargets.Property | System.AttributeTargets.ReturnValue)]
public class XmlNamespaceDeclarationsAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.Field | System.AttributeTargets.Parameter | System.AttributeTargets.Property | System.AttributeTargets.ReturnValue, AllowMultiple=false)>]
type XmlNamespaceDeclarationsAttribute = class
    inherit Attribute
[<System.AttributeUsage(System.AttributeTargets.Field | System.AttributeTargets.Parameter | System.AttributeTargets.Property | System.AttributeTargets.ReturnValue)>]
type XmlNamespaceDeclarationsAttribute = class
    inherit Attribute
Public Class XmlNamespaceDeclarationsAttribute
Inherits Attribute
상속
XmlNamespaceDeclarationsAttribute
특성

설명

특성은 XmlNamespaceDeclarationsAttribute 개체를 반환하는 필드 또는 속성에 클래스에서 한 번만 적용할 수 있습니다 XmlSerializerNamespaces .

XML XmlNamespaceDeclarationsAttribute 문서에서 사용되는 접두사 및 연결된 네임스페이스를 저장할 수 있습니다. 예를 들어 특성의 일반적인 용도 중 하나는 XPath(XML Language) 버전 1.0이라는 World Wide Web 컨소시엄 문서에 정의되어 있으므로 XPath 데이터를 저장하는 것입니다. 간단히 말해 XPath는 다른 구문과 함께 많은 네임스페이스 접두사 및 로컬 이름을 포함하는 문자열입니다.

XPath 언어를 사용하면 접두사를 경로와 연결하고 XML 문서 내의 접두사를 사용할 수 있습니다. 예를 들어 "select"라는 다음 XML 문서에는 특정 URIhttp://www.cohowinery.com/calendar/()와 연결된 접두사("cal")가 포함되어 있습니다. 요소에는 XPath를 포함하는 "path"라는 특성이 포함되어 있습니다.

<select xmlns:cal ="http://www.cohowinery.com/calendar/" path="cal:appointments/@startTime" />

이에 대한 스키마는 다음과 같습니다.

<element name="select">
   <complexType>
      <simpleContent>
         <attribute name="path" />
      </simpleContent>
   </complexType>
</element>

XmlNamespaceDeclarationsAttribute없으면 접두사와 네임스페이스 간의 연결이 손실됩니다.

접두사와 네임스페이스 URI 간의 연결을 유지하려면 다음 C# 및 Visual Basic 코드와 같이 XmlSerializerNamespaces 개체를 반환하고 XmlNamespaceDeclarationsAttribute 특성을 멤버에 적용하는 멤버를 추가합니다.

// C#
public class Select {
  [XmlAttribute] public string path;
  [XmlNamespaceDeclarations] public XmlSerializerNamespaces xmlns;
}
' Visual Basic
Public Class Select
   <XmlAttribute> Public path As String
   <XmlNamespaceDeclarations> Public xmlns As XmlSerializerNamespaces
End Class

serialize할 때 생성된 XML 문서의 스키마에는 이름이 XSD appinfo(XML 스키마 정의) 요소가 포함됩니다. 요소에는 네임스페이스 선언이 포함된 멤버의 이름으로 설정된 메타데이터 keepNamespaceDeclarations요소가 추가로 포함됩니다. 다음 XML 조각은 스키마를 보여줍니다.

<xs:element name="select">
   <xs:complexType>
      <xs:annotation>
         <xs:appinfo>
          <keepNamespaceDeclarations>xmlns</keepNamespaceDeclarations>
         </xs:appinfo>
      </xs:annotation>
      <xs:simpleContent>
         <xs:attribute name="path" />
      </xs:simpleContent>
   </xs:complexType>
</xs:element>

역직렬화할 때 xmlns 필드에는 XmlSerializerNamespaces 모든 네임스페이스 접두사 정의가 포함된 개체가 포함됩니다.

serialization에서 사용자는 메서드를 사용하여 개체에 접두사 네임스페이 XmlSerializerNamespaces 스 쌍을 Add 추가할 수 있습니다. 다음 C# 및 Visual Basic 코드에 나와 있습니다.

// C#
using System;
using System.IO;
using System.Xml.Serialization;
[XmlRoot("select")]
public class Select {
   [XmlAttribute]
   public string xpath;
   [XmlNamespaceDeclarations]
   public XmlSerializerNamespaces xmlns;
}
public class Test {
   public static void Main(string[] args) {
      Select mySelect = new Select();
      mySelect.xpath = "myNS:ref/@common:y";
      mySelect.xmlns = new XmlSerializerNamespaces();
      mySelect.xmlns.Add("MyNS", "myNS.tempuri.org");
      mySelect.xmlns.Add("common", "common.tempuri.org");
      XmlSerializer ser = new XmlSerializer(typeof(Select));
      ser.Serialize(Console.Out, mySelect);
   }
}
// Output:
// <?xml version="1.0" encoding="IBM437"?>
// <select xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
// xmlns:common="common.tempuri.org" xmlns:MyNS="myNS.tempuri.org" xpath="myNS:ref/@common:y" />
' Visual Basic
Imports System
Imports System.IO
Imports System.Xml.Serialization
<XmlRoot("select")> _
Public Class SelectPath
   <XmlAttribute> _
   Public xpath As String
   <XmlNamespaceDeclarations> _
   public xmlns As XmlSerializerNamespaces
End Class
Public Class Test
   Public Shared Sub Main()
      Dim mySelect As SelectPath = New SelectPath()
      mySelect.xpath = "myNS:ref/@common:y"
      mySelect.xmlns = New XmlSerializerNamespaces()
      mySelect.xmlns.Add("MyNS", "myNS.tempuri.org")
      mySelect.xmlns.Add("common", "common.tempuri.org")
      Dim ser As XmlSerializer = New XmlSerializer(mySelect.GetType)
      ser.Serialize(Console.Out, mySelect)
   End Sub
End Class
'Output:
' <?xml version="1.0" encoding="IBM437"?>
' <select xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
' xmlns:common="common.tempuri.org" xmlns:MyNS="myNS.tempuri.org" xpath="myNS:ref/@common:y" />

또한 특성이 적용되는 멤버에는 클래스에서 정의한 XML 요소에 속하는 접두사 네임스페이스 쌍만 포함됩니다. 예를 들어 다음 XML 문서에서는 접두사 쌍 "cal"만 캡처되지만 "x" 접두사는 캡처되지 않습니다. 해당 데이터를 얻으려면 요소를 나타내는 클래스에 XmlNamespaceDeclarationsAttribute 멤버를 추가합니다 root .

<?xml version="1.0"?>
<x:root xmlns:x="http://www.cohowinery.com/x/">
  <x:select xmlns:cal="http://www.cohowinery.com/calendar/" path="cal:appointments/@cal:startTime" />
</x:root>

생성자

Name Description
XmlNamespaceDeclarationsAttribute()

XmlNamespaceDeclarationsAttribute 클래스의 새 인스턴스를 초기화합니다.

속성

Name Description
TypeId

파생 클래스에서 구현되는 경우 이 Attribute대한 고유 식별자를 가져옵니다.

(다음에서 상속됨 Attribute)

메서드

Name Description
Equals(Object)

이 인스턴스가 지정된 개체와 같은지 여부를 나타내는 값을 반환합니다.

(다음에서 상속됨 Attribute)
GetHashCode()

이 인스턴스의 해시 코드를 반환합니다.

(다음에서 상속됨 Attribute)
GetType()

현재 인스턴스의 Type 가져옵니다.

(다음에서 상속됨 Object)
IsDefaultAttribute()

파생 클래스에서 재정의되는 경우 이 인스턴스의 값이 파생 클래스의 기본값인지 여부를 나타냅니다.

(다음에서 상속됨 Attribute)
Match(Object)

파생 클래스에서 재정의되는 경우 이 인스턴스가 지정된 개체와 같은지 여부를 나타내는 값을 반환합니다.

(다음에서 상속됨 Attribute)
MemberwiseClone()

현재 Object단순 복사본을 만듭니다.

(다음에서 상속됨 Object)
ToString()

현재 개체를 나타내는 문자열을 반환합니다.

(다음에서 상속됨 Object)

명시적 인터페이스 구현

Name Description
_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

이름 집합을 해당 디스패치 식별자 집합에 매핑합니다.

(다음에서 상속됨 Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

인터페이스의 형식 정보를 가져오는 데 사용할 수 있는 개체의 형식 정보를 검색합니다.

(다음에서 상속됨 Attribute)
_Attribute.GetTypeInfoCount(UInt32)

개체가 제공하는 형식 정보 인터페이스의 수를 검색합니다(0 또는 1).

(다음에서 상속됨 Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

개체에 의해 노출되는 속성 및 메서드에 대한 액세스를 제공합니다.

(다음에서 상속됨 Attribute)

적용 대상