XPathQueryGenerator Class

.NET Framework (current version)

The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

When given a class representing a data contract, and metadata representing a member of the contract, produces an XPath query for the member.

Namespace:   System.Runtime.Serialization
Assembly:  System.Runtime.Serialization (in System.Runtime.Serialization.dll)


public static class XPathQueryGenerator

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

Creates an XPath from a data contract using the specified contract data type, array of metadata elements, the top level element, and namespaces.

System_CAPS_pubmethodSystem_CAPS_staticCreateFromDataContractSerializer(Type, MemberInfo[], XmlNamespaceManager)

Creates an XPath from a data contract using the specified data contract type, array of metadata elements, and namespaces..

For more information about data contracts, see Using Data Contracts.

To use the class, there are four steps:

  1. Create the data contract type by applying the DataContractAttribute and DataMemberAttribute as appropriate to the type and its fields or properties.

  2. Use the GetMember method of the Type class to generate the MemberInfo array.

  3. Pass the type and the array to the method.

  4. If needed, use the XmlNamespaceManager returned by the namespaces parameter to examine the XML namespaces referenced by the namespaces prefixes in the XPath query.


The namespace prefix "xg" (for "XPath Generator") is used as the default in the XPath. This cannot be changed. Instead, refer to the NameTable collection to see what namespace the prefix is associated with.

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);


        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);

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



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

        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; }

.NET Framework
Available since 4.0

Any public static ( Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Return to top