Share via


XPathQueryGenerator 클래스

정의

데이터 계약을 나타내는 클래스와 계약의 멤버를 나타내는 메타데이터가 지정되면 멤버에 대한 XPath 쿼리를 생성합니다.

public ref class XPathQueryGenerator abstract sealed
public static class XPathQueryGenerator
type XPathQueryGenerator = class
Public Class XPathQueryGenerator
상속
XPathQueryGenerator

예제

다음 예제에서는 및 DataMemberAttribute 특성이 적용된 두 클래스에서 XPath 쿼리를 DataContractAttribute 만듭니다.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Runtime.Serialization;
using System.Xml;

namespace GeneratPathExample
{
    class Program
    {
        static void Main(string[] args)
        {
            // Get the type of the class that defines the data contract.
            Type t = typeof(Order);

            // Get the meta data for the specific members to be used in the query.
            MemberInfo[] mi = t.GetMember("Product");
            MemberInfo[] mi2 = t.GetMember("Value");
            MemberInfo[] mi3 = t.GetMember("Quantity");

            // Call the function below to generate and display the query.
            GenerateXPath(t, mi);
            GenerateXPath(t, mi2);
            GenerateXPath(t, mi3);

            // Get the type of the second class that defines a data contract.
            Type t2 = typeof(Line);

            // Get the meta data for the member to be used in the query.
            MemberInfo[] mi4 = t2.GetMember("Items");

            GenerateXPath(t2, mi4);

            Console.ReadLine();
        }

        static void GenerateXPath(Type t, MemberInfo[] mi)
        {

            // Create a new name table and name space manager.
            NameTable nt = new NameTable();
            XmlNamespaceManager xname = new XmlNamespaceManager(nt);

            // Generate the query and print it.
            string query = XPathQueryGenerator.CreateFromDataContractSerializer(
                t, mi, out xname);
            Console.WriteLine(query);
            Console.WriteLine();

            // Display the namespaces and prefixes used in the data contract.
            foreach (string s in xname)
                Console.WriteLine("{0}  = {1}", s, xname.LookupNamespace(s));

            Console.WriteLine();
        }
    }

    [DataContract(Namespace = "http://www.cohowinery.com/")]
    public class Line
    {
        private Order[] itemsValue;

        [DataMember]
        public Order[] Items
        {
            get { return itemsValue; }
            set { itemsValue = value; }
        }
    }

    [DataContract(Namespace = "http://contoso.com")]
    public class Order
    {
        private string productValue;
        private int quantityValue;
        private decimal valueValue;

        [DataMember(Name = "cost")]
        public decimal Value
        {
            get { return valueValue; }
            set { valueValue = value; }
        }

        [DataMember(Name = "quantity")]
        public int Quantity
        {
            get { return quantityValue; }
            set { quantityValue = value; }
        }

        [DataMember(Name = "productName")]
        public string Product
        {
            get { return productValue; }
            set { productValue = value; }
        }
    }
}
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.Reflection
Imports System.Runtime.Serialization
Imports System.Xml

Namespace GeneratPathExample

    Class Program

        Shared Sub Main(ByVal args As String())

            ' Get the type of the class that defines the data contract.
            Dim t As Type = GetType(Order)

            ' Get the meta data for the specific members to be used in the query.
            Dim mi As MemberInfo() = t.GetMember("Product")
            Dim mi2 As MemberInfo() = t.GetMember("Value")
            Dim mi3 As MemberInfo() = t.GetMember("Quantity")

            ' Call the function below to generate and display the query.
            GenerateXPath(t, mi)
            GenerateXPath(t, mi2)
            GenerateXPath(t, mi3)


            ' Get the type of the second class that defines a data contract.
            Dim t2 As Type = GetType(Line)

            ' Get the meta data for the member to be used in the query.
            Dim mi4 As MemberInfo() = t2.GetMember("Items")

            GenerateXPath(t2, mi4)

            Console.ReadLine()
        End Sub

        Shared Sub GenerateXPath(ByVal t As Type, ByVal mi As MemberInfo())


            ' Create a new name table and name space manager.
            Dim nt As New NameTable()
            Dim xname As New XmlNamespaceManager(nt)


            ' Generate the query and print it.
            Dim query As String = XPathQueryGenerator.CreateFromDataContractSerializer( _
                t, mi, xname)
            Console.WriteLine(query)
            Console.WriteLine()


            ' Display the namespaces and prefixes used in the data contract.
            Dim s As String
            For Each s In xname
                Console.WriteLine("{0}  = {1}", s, xname.LookupNamespace(s))
            Next

            Console.WriteLine()

        End Sub
    End Class


    <DataContract(Namespace:="http://www.cohowinery.com/")> _
        Public Class Line

        Private itemsValue As Order()

        <DataMember()>
        Public Property Item() As Order()

            Get
                Return itemsValue
            End Get
            Set(ByVal value As Order())
                itemsValue = value
            End Set
        End Property

    End Class

    <DataContract(Namespace:="http://contoso.com")> _
    Public Class Order

        Private productValue As String
        Private quantityValue As Integer
        Private valueValue As Decimal

        <DataMember(Name:="cost")>
        Public Property Value() As String

            Get
                Return valueValue
            End Get
            Set(ByVal value As String)
                valueValue = value
            End Set
        End Property

        <DataMember(Name:="quantity")> _
        Public Property Quantity() As Integer

            Get
                Return quantityValue
            End Get
            set(ByVal value As Integer)
                quantityValue = value
            End Set
        End Property


        <DataMember(Name:="productName")> _
        Public Property Product() As String

            Get
                Return productValue
            End Get
            Set(ByVal value As String)
                productValue = value
            End Set
        End Property
    End Class
End Namespace

설명

데이터 계약에 대한 자세한 내용은 데이터 계약 사용을 참조하세요.

클래스를 사용하려면 다음 네 단계가 있습니다.

  1. 및 를 형식 및 해당 필드 또는 속성에 적절하게 적용하여 DataContractAttributeDataMemberAttribute 데이터 계약 형식을 만듭니다.

  2. 클래스의 GetMember 메서드를 Type 사용하여 MemberInfo 배열을 생성합니다.

  3. 형식 및 배열을 메서드에 전달합니다.

  4. 필요한 경우 매개 변수에서 XmlNamespaceManager 반환된 를 namespaces 사용하여 XPath 쿼리의 네임스페이스 접두사에서 참조하는 XML 네임스페이스를 검사합니다.

참고

네임스페이스 접두사 "xg"("XPath 생성기"의 경우)는 XPath에서 기본값으로 사용됩니다. 이 설정은 변경할 수 없습니다. 대신 NameTable 컬렉션을 참조하여 접두사에 연결된 네임스페이스를 확인합니다.

메서드

CreateFromDataContractSerializer(Type, MemberInfo[], StringBuilder, XmlNamespaceManager)

지정된 계약 데이터 형식, 메타데이터 요소 배열, 최상위 요소 및 네임스페이스를 사용하여 데이터 계약에서 XPath를 만듭니다.

CreateFromDataContractSerializer(Type, MemberInfo[], XmlNamespaceManager)

지정된 데이터 계약 형식, 메타데이터 요소 배열 및 네임스페이스를 사용하여 데이터 계약에서 XPath를 만듭니다.

적용 대상