BizTalk Utilities CV ,   Jobs ,   Code library  
 
Home Page
SQL XML
Manually inserting SQLXML data
.NET DataSet Class
SQLXML XPath Queries
.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
<< 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 :251

 

Using OPENXML in .NET

The second approach is a slight evolution of the first one. Instead of writing .NET code to extract the data from the XML document we write SQL code containing a SELECT statement combined with the OPENXML clause as shown in the next method.

public static void InsertOrderHeaderWithOPENXML( string xml,

  string connectionString )

{

 string statement = String.Format(

    @"DECLARE @idoc int

    DECLARE @doc varchar(1000)

    SET @doc ='{0}'

    EXEC sp_xml_preparedocument @idoc OUTPUT, @doc

    INSERT OrderHeader ( OrderID, CustomerID )

    SELECT  OrderID, CustomerID

    FROM OPENXML (@idoc, '/OrderHeader',2)

      WITH (

      OrderID int,

      CustomerID int)

    EXEC sp_xml_removedocument @idoc", xml );

    ExecuteNonQuerySQL( statement, connectionString );   

}

Coding effort and maintenance liability for this technique are very similar to the previous one. The SQL code in this method is just as much special purpose as the C# code that composed the SQL from the content of an XmlDocument. The opportunity for re-using this function in any other context than inserting data from this particular XML format is zero. Handling more complex XML formats is also possible. We would simple add more INSERT…SELECT … OPENXML …WITH statements to insert data into more than one table.

The performance characteristics are slightly different than the last one. On the plus side, you don’t need as much memory on the application server because you are not parsing the XML document to compose a SQL statement. On the other hand, you need the extra memory and the processing cycles on your database server, which lowers the overall scalability of your application. Of course this point is moot if your application is not deployed to separate servers.

Depending on your company’s database philosophy you may want to move the SQL code into a stored procedure, which would yield better separation of XML conversion and database access code.  In some workplaces, however access to databases is much more restricted than access to an application server, in which case you might rather want to create SQL in your .NET components than executing stored procedures.


Rate this article on a scale of 1 to 10

Your vote :  


 

Recent Jobs

Sr. Software Engineer - Analytics
Immediate Mainframe openings for Ch
Immediate TANDEM-TAL openings for C
Immediate ASP.NET/C# Openings for C
Sr. Software Engineer

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



United Kingdom Conference Calling
water softener
Teleconference
Host Department NOLIMIT Web Hosting
MSN
sunglasses
online fax


    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