XPathQueryGenerator 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
當提供代表資料合約的類別,以及代表合約成員的中繼資料時,會產生該成員的 XPath 查詢。
public ref class XPathQueryGenerator abstract sealed
public static class XPathQueryGenerator
type XPathQueryGenerator = class
Public Class XPathQueryGenerator
- 繼承
-
XPathQueryGenerator
範例
下列範例會從兩個類別建立 XPath 查詢,這兩個類別均已套用 DataContractAttribute 和 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
備註
如需資料合約的詳細資訊,請參閱 使用資料合約。
使用類別有四個步驟:
視需要,將 DataContractAttribute 和 DataMemberAttribute 套用至型別及其欄位或屬性,以建立資料合約型別。
將型別及陣列貼至方法。
如有需要,請使用 XmlNamespaceManager 參數所傳回的
namespaces
檢查 XPath 查詢中的命名空間前置詞參考的 XML 命名空間。
注意
XPath 中預設會使用命名空間前置詞 "xg" (表示「XPath 產生器」)。 這無法變更。 請改為參考 NameTable 集合,查看與該前置詞關聯的命名空間。
方法
CreateFromDataContractSerializer(Type, MemberInfo[], StringBuilder, XmlNamespaceManager) |
使用指定的資料合約型別、中繼資料項目陣列、最上層項目及命名空間,從資料合約建立 XPath。 |
CreateFromDataContractSerializer(Type, MemberInfo[], XmlNamespaceManager) |
使用指定的資料合約類型、中繼資料項目陣列及命名空間,從資料合約建立 XPath。 |