Executing XPath Queries (SQLXML Managed Classes)
SQL Server 2000
Executing XPath Queries (SQLXML Managed Classes)
This example illustrates how XPath queries are executed against a mapping schema.
Consider this schema:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:sql="urn:schemas-microsoft-com:mapping-schema">
<xsd:element name="Emp" sql:relation="Employees" >
<xsd:complexType>
<xsd:sequence>
<xsd:element name="FName"
sql:field="FirstName"
type="xsd:string" />
<xsd:element name="LName"
sql:field="LastName"
type="xsd:string" />
</xsd:sequence>
<xsd:attribute name="EmployeeID" type="xsd:integer" />
</xsd:complexType>
</xsd:element>
</xsd:schema>
This C# application executes an XPath query against this schema (MySchema.xml).
In the code, you must provide the name of the instance of Microsoft® SQL Server™ in the connection string.
using System;
using Microsoft.Data.SqlXml;
using System.IO;
class Test
{
static string NorthwindConnString = "Provider=SQLOLEDB;Server=(local);database=Northwind;Integrated Security=SSPI";
public static int testXPath()
{
Stream strm;
SqlXmlCommand cmd = new SqlXmlCommand(NorthwindConnString);
cmd.CommandText = "Emp";
cmd.CommandType = SqlXmlCommandType.XPath;
cmd.RootTag = "ROOT";
cmd.SchemaPath = "MySchema.xml";
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
To test this example, you must have the Microsoft .NET Framework installed on your computer.
- 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.csThis creates an executable (DocSample.exe).
- At the command prompt, execute DocSample.exe.
