Executing XPath Queries with Namespaces (SQLXML Managed Classes)
XPath queries can include namespaces. If the schema elements are namespace-qualified (use a target namespace), the XPath queries against the schema must specify the namespace.
Because the wildcard character (*) is not supported in Microsoft SQLXML 4.0, you must specify the XPath query by using a namespace prefix. To resolve the prefix, use the namespaces property to specify the namespace binding.
In the following example, the XPath query specifies namespaces by using the wildcard character (*) and the local-name() and namespace-uri() XPath functions. This XPath query returns all the elements where the local name is Employee and the namespace URI is urn:myschema:Contacts:
/*[local-name() = 'Contact' and namespace-uri() = 'urn:myschema:Contacts']
In SQLXML 4.0, specify this XPath query with a namespace prefix. An example is x:Person, where x is the namespace prefix. Consider the following XSD schema:
<schema xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:sql="urn:schemas-microsoft-com:mapping-schema"
xmlns:con="urn:myschema:Contacts"
targetNamespace="urn:myschema:Contact">
<complexType name="ContactType">
<attribute name="CID" sql:field="BusinessEntityID" type="ID"/>
<attribute name="FName" sql:field="FirstName" type="string"/>
<attribute name="LName" sql:field="LastName"/>
</complexType>
<element name="Person" type="con:PersonType" sql:relation="Person.Person"/>
</schema>
Because this schema defines the target namespace, an XPath query (such as "Employee") against this schema must include the namespace.
The following C# sample application executes an XPath query against the preceding XSD schema (MySchema.xml). To resolve the prefix, specify the namespace binding by using the Namespaces property of the SqlXmlCommand object.
Note
In the code, you must provide the name of the instance of SQL Server in the connection string.
using System;
using Microsoft.Data.SqlXml;
using System.IO;
class Test
{
static string ConnString = "Provider=SQLOLEDB;Server=(local);database=AdventureWorks2008R2;Integrated Security=SSPI";
public static int testXPath()
{
//Stream strm;
SqlXmlCommand cmd = new SqlXmlCommand(ConnString);
cmd.CommandText = "x:Contact[@CID='1']";
cmd.CommandType = SqlXmlCommandType.XPath;
cmd.RootTag = "ROOT";
cmd.Namespaces = "xmlns:x='urn:myschema:ContactsContact'";
cmd.SchemaPath = "MySchema.xml";
using (Stream strm = cmd.ExecuteStream()){
using (StreamReader sr = new StreamReader(strm)){
Console.WriteLine(sr.ReadToEnd());
}
}
return 0;
}
public static int Main(String[] args)
{
testXPath();
return 0;
}
}
To test the application
Ensure that the Microsoft .NET Framework is installed on your computer. It is required to test this example.
Save the XSD schema (MySchema.xml) that is provided in this example in a folder.
Save the C# code (DocSample.cs) that is provided in this example in the same folder in which the schema is stored. (If you store the files in a different folder, you will have to edit the code and specify the appropriate directory path for the mapping schema.)
Compile the code. To compile the code at the command prompt, use:
csc /reference:Microsoft.Data.SqlXML.dll DocSample.cs
This creates an executable (DocSample.exe).
At the command prompt, execute DocSample.exe.