XPath 쿼리 실행(SQLXML 관리되는 클래스)

적용 대상: SQL Server Azure SQL 데이터베이스

이 예에서는 매핑 스키마에 대해 XPath 쿼리를 실행하는 방법을 보여 줍니다.

다음 스키마를 참조하십시오.

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"  
            xmlns:sql="urn:schemas-microsoft-com:mapping-schema">  
  <xsd:element name="Con" sql:relation="Person.Contact" >  
   <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="ContactID" type="xsd:integer" />  
    </xsd:complexType>  
  </xsd:element>  
</xsd:schema>  

이 C# 애플리케이션에서는 이 스키마(MySchema.xml)에 대해 XPath 쿼리를 실행합니다.

참고

코드에서 연결 문자열에 Microsoft SQL Server instance 이름을 제공해야 합니다.

using System;  
using Microsoft.Data.SqlXml;  
using System.IO;  
class Test  
{  
      static string ConnString = "Provider=SQLOLEDB;Server=(local);database=AdventureWorks;Integrated Security=SSPI";  
  
      public static int testXPath()  
      {  
         Stream strm;  
         SqlXmlCommand cmd = new SqlXmlCommand(ConnString);  
         cmd.CommandText = "Con";  
         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;  
      }  
   }  

애플리케이션을 테스트하려면

  1. 컴퓨터에 Microsoft .NET Framework 설치되어 있는지 확인합니다.

  2. 이 예에서 제공하는 XSD 스키마(MySchema.xml)를 폴더에 저장합니다.

  3. 이 예에서 제공하는 C# 코드(DocSample.cs)를 스키마가 저장된 폴더와 같은 폴더에 저장합니다. (다른 폴더에 파일을 저장하는 경우 코드를 편집하여 매핑 스키마에 대해 적합한 디렉터리 경로를 지정해야 합니다.)

  4. 코드를 컴파일합니다. 다음을 사용하여 명령 프롬프트에서 코드를 컴파일합니다.

    csc /reference:Microsoft.Data.SqlXML.dll DocSample.cs  
    

    이렇게 하면 실행 파일(DocSample.exe)이 만들어집니다.

  5. 명령 프롬프트에서 DocSample.exe를 실행합니다.