XPathQueryGenerator XPathQueryGenerator XPathQueryGenerator XPathQueryGenerator Class

定義

データ コントラクトを表すクラス、およびそのコントラクトのメンバーを表すメタデータを指定すると、該当するメンバーの XPath クエリが生成されます。When given a class representing a data contract, and metadata representing a member of the contract, produces an XPath query for the member.

public ref class XPathQueryGenerator abstract sealed
public static class XPathQueryGenerator
type XPathQueryGenerator = class
Public Class XPathQueryGenerator
継承
XPathQueryGeneratorXPathQueryGeneratorXPathQueryGeneratorXPathQueryGenerator

DataContractAttribute 属性と DataMemberAttribute 属性が適用された 2 つのクラスから XPath クエリを作成する例を次に示します。The following example creates XPath queries from two classes to which the DataContractAttribute and DataMemberAttribute attributes have been applied.

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

注釈

データコントラクトの詳細については、「データコントラクトの使用」を参照してください。For more information about data contracts, see Using Data Contracts.

クラスを使用する場合の手順は次の 4 つです。To use the class, there are four steps:

  1. その型およびフィールドやプロパティに対し DataContractAttribute および DataMemberAttribute を適切に適用して、データ コントラクト型を作成します。Create the data contract type by applying the DataContractAttribute and DataMemberAttribute as appropriate to the type and its fields or properties.

  2. GetMember クラスの Type メソッドを使用して、MemberInfo 配列を生成します。Use the GetMember method of the Type class to generate the MemberInfo array.

  3. メソッドに型と配列を渡します。Pass the type and the array to the method.

  4. 必要に応じて、XmlNamespaceManager パラメーターによって返される namespaces を使用し、XPath クエリ内の名前空間プレフィックスによって参照される XML 名前空間を確認します。If needed, use the XmlNamespaceManager returned by the namespaces parameter to examine the XML namespaces referenced by the namespaces prefixes in the XPath query.

注意

名前空間プレフィックス "xg" ("XPath ジェネレーター") は XPath の既定値として使用されます。The namespace prefix "xg" (for "XPath Generator") is used as the default in the XPath. これは変更できません。This cannot be changed. 代わりに NameTable コレクションを参照し、プレフィックスが関連付けられている名前空間を確認します。Instead, refer to the NameTable collection to see what namespace the prefix is associated with.

メソッド

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

指定したデータ コントラクト型、メタデータ要素の配列、および名前空間を使用して、データ コントラクトから XPath を作成します。Creates an XPath from a data contract using the specified data contract type, array of metadata elements, and namespaces.

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

指定したコントラクト データ型、メタデータ要素の配列、トップ レベルの要素、および名前空間を使用して、データ コントラクトから XPath を作成します。Creates an XPath from a data contract using the specified contract data type, array of metadata elements, the top level element, and namespaces.

適用対象