This site has been taken over by the staff of www.ASPDeveloper.Net

Please report errors to suggest@aspdeveloper.net

BizTalk Utilities CV ,   Jobs ,   Code library  
 
Home Page
SQL XML
.NET XmlTextWriter
SQLXML - XML enabling the Data Layer
.NET SqlXmlException Class
.NET SqlXmlAdapter Classes
.NET SqlXmlParameter Class
.NET SqlXmlCommand Class
.NET Managed Classes
Annotating .NET Updategrams
SQLXML - .NET Updategrams
.NET XPath Queries
SQLXML - Mapping Relationships To XML
SQLXML - Mapping .NET Schemas
.NET & OPENXML
.NET Parameterized Templates
SQLXML - Raw mode
SQLXML - FOR XML mode
Automate saving options from ADO into an XML format
Microsoft's dataselect functionality in the XML DSO
VB/ADO to DataIsland
Inserting nodes: String Load or Dom Load?
<< SEO
System.XML >>

By :Mark Wilson
I am the creator of TopXML. I am available for international and local (Australia) contracts. I am a Solution Architect/Business Analyst. I have worked in IT in several countries (NZ, Australia, South Africa, UK) building and training teams for government and very large non-governmental organizations. I am ex-Microsoft Consulting Services. I wrote the first book on Microsoft XML published in 2000 called XML Programming with VB and ASP. Most recently I have been building tools for the SEO industry. Ask me for a 37 point SEO health-checkup for your website.
First posted :03/24/2008
Times viewed :2617

 

SQLXML XPath Queries

One slight change to the last approach to buy us an extra edge on the performance side would be to replace the SqlCommand with the SqlXmlCommand from the SQLXML library. The SqlXmlCommand uses the SQLXML OLE DB provider which performs better than the SQL Server library used by the SqlCommand class when it comes to retrieving XML. Combined with the either the StreamReader or the XmlTextWriter this combination will outperform all other approaches we examine in this section. Switching to the SqlXmlCommand also enables client-side XML formatting, i.e. we can move the load associated with the transformation of the results to XML from the data base server to the application server. We’ll examine client-side formatting more detailed in section 15.3.3.1.

In the next approach we replace SQL queries with XPath expressions and annotated XML schemas. This approach is a great compromise between ease-of-development, speed, flexibility and maintainable code. The code is easy to maintain because all the mapping logic is maintained in the annotated schema outside the compiled code reducing the cost of changes to the mapping logic to a minimum. XPath queries are also significantly simpler to develop than FOR XML EXPLICIT queries. Sounds great already, but there is more. The annotations allow a great deal of flexibility how the SQL Server table structure maps to XML and we are still using the fast SQLXML OLE DB provider under the hood. If you are concerned about the impact of interpreting the schema and transforming the XPath expression to a FOR XML EXPLICIT query, you might feel better knowing that SQLXML caches the result of a schema’s analysis. After the initial hit parsing the schema, XPath queries are similar in performance to the first approach, manually transforming results of a “regular” SQL query from the returned DataReader. The code example below shows that executing an XPath query is just as simple as in the previous example.

public static void ExecuteXPathQuery( string xpathQuery, string

  connectionString, string schemaPath, string rootName,

  ref Stream stream )

{

  SqlXmlCommand cmd = new SqlXmlCommand(connectionString);     

  cmd.CommandText = xpathQuery;

  cmd.CommandType = SqlXmlCommandType.XPath;

  cmd.RootTag = rootName;

  cmd.SchemaPath = schemaPath;

  cmd.ExecuteToStream( stream );

}

With only a few generic lines we can execute the XPath expression and return the results in XML. The example below makes use of the SqlXmlCommand class’ ExecuteToStream() method, which can speed up returning the results another notch. The XML transformation of the results happens now straight into the stream that is returned to the application, whereas the first two techniques required copying the query results to a separate output stream (wrapped by an XmlTextWriter). Just like the previous approach the SqlXmlCommand allows client-side formatting, which reduces the load we put on the database server.


Rate this article on a scale of 1 to 10

Your vote :  


 

Recent Jobs

Software Specialist, Linux - Finlan
Linux Core Technical Project Manage
Graphics designer at Tanzania. Expe
Integration Specialist Needed - Wor
Virtualization Server Infrastructur

View all Jobs (Add yours)
View all CV (Add yours)






    Email TopXML  

Front Page Daily Stuff TopXML Forum XML blogs XML Newsgroups BizTalk Biztalk Utilities Biztalk Utilities Tutorial B2B SAP XML Microsoft .NET Dotnet System XML Soapformatter SQLXML XMLserializer XQuery PHP PHP SimpleXML PHP XML Dom PHP XML RPC PHP XSLT Java Java Java XML Xalan Microsoft ASP ASP Schemas XML SQL Server XML XMLDom XSL XSL Tutorial XSLT Stylesheets General Javascript CSS XHTML WAP