XPathQueryGenerator.CreateFromDataContractSerializer Método

Definición

Crea un XPath a partir de un contrato de datos.

Sobrecargas

CreateFromDataContractSerializer(Type, MemberInfo[], XmlNamespaceManager)

Crea un elemento XPath a partir de un contrato de datos con los elementos especificados (tipo de contrato de datos, matriz de elementos de metadatos y espacios de nombres).

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

Crea un XPath a partir de un contrato de datos mediante el tipo de datos del contrato, la matriz de elementos de metadatos, el elemento de nivel superior y los espacios de nombres especificados.

CreateFromDataContractSerializer(Type, MemberInfo[], XmlNamespaceManager)

Crea un elemento XPath a partir de un contrato de datos con los elementos especificados (tipo de contrato de datos, matriz de elementos de metadatos y espacios de nombres).

public:
 static System::String ^ CreateFromDataContractSerializer(Type ^ type, cli::array <System::Reflection::MemberInfo ^> ^ pathToMember, [Runtime::InteropServices::Out] System::Xml::XmlNamespaceManager ^ % namespaces);
public static string CreateFromDataContractSerializer (Type type, System.Reflection.MemberInfo[] pathToMember, out System.Xml.XmlNamespaceManager namespaces);
static member CreateFromDataContractSerializer : Type * System.Reflection.MemberInfo[] * XmlNamespaceManager -> string
Public Shared Function CreateFromDataContractSerializer (type As Type, pathToMember As MemberInfo(), ByRef namespaces As XmlNamespaceManager) As String

Parámetros

type
Type

Tipo que representa un contrato de datos.

pathToMember
MemberInfo[]

Los metadatos, generados utilizando el método GetMember de la clase Type que señala al miembro de datos concreto usado para generar la consulta.

namespaces
XmlNamespaceManager

Los espacios de nombres XML y sus prefijos situados en el contrato de datos.

Devoluciones

String

String

El XPath generado a partir de los datos de miembro y tipo.

Ejemplos

En el siguiente ejemplo se crean las consultas XPath a partir de dos clases a las que se han aplicado los atributos DataContractAttribute y DataMemberAttribute.

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

Comentarios

Para obtener más información sobre los contratos de datos, consulte Uso de contratos de datos.

Se aplica a

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

Crea un XPath a partir de un contrato de datos mediante el tipo de datos del contrato, la matriz de elementos de metadatos, el elemento de nivel superior y los espacios de nombres especificados.

public:
 static System::String ^ CreateFromDataContractSerializer(Type ^ type, cli::array <System::Reflection::MemberInfo ^> ^ pathToMember, System::Text::StringBuilder ^ rootElementXpath, [Runtime::InteropServices::Out] System::Xml::XmlNamespaceManager ^ % namespaces);
public static string CreateFromDataContractSerializer (Type type, System.Reflection.MemberInfo[] pathToMember, System.Text.StringBuilder? rootElementXpath, out System.Xml.XmlNamespaceManager namespaces);
public static string CreateFromDataContractSerializer (Type type, System.Reflection.MemberInfo[] pathToMember, System.Text.StringBuilder rootElementXpath, out System.Xml.XmlNamespaceManager namespaces);
static member CreateFromDataContractSerializer : Type * System.Reflection.MemberInfo[] * System.Text.StringBuilder * XmlNamespaceManager -> string
Public Shared Function CreateFromDataContractSerializer (type As Type, pathToMember As MemberInfo(), rootElementXpath As StringBuilder, ByRef namespaces As XmlNamespaceManager) As String

Parámetros

type
Type

Tipo que representa un contrato de datos.

pathToMember
MemberInfo[]

Los metadatos, generados utilizando el método GetMember de la clase Type que señala al miembro de datos concreto usado para generar la consulta.

rootElementXpath
StringBuilder

El elemento de nivel superior en el xpath.

namespaces
XmlNamespaceManager

Los espacios de nombres XML y sus prefijos situados en el contrato de datos.

Devoluciones

String

String

El XPath generado a partir de los datos de miembro y tipo.

Se aplica a