xml file validation with xsdhttp://forums.asp.net/t/1325687.aspx/1?xml+file+validation+with+xsdWed, 08 Oct 2008 19:20:04 -040013256872646316http://forums.asp.net/p/1325687/2646316.aspx/1?xml+file+validation+with+xsdxml file validation with xsd <p>hi,</p> <p>i am inserting data from xml into sql 2005 table.</p> <p>now before reading xml file i want to validate the xml file with the schema.</p> <p>if validated i want to open and do my operation.</p> <p>else i want to log the error.</p> <p>any suggestions...</p> <p>i am able to insert the xml data into database table(with openxml) &nbsp;but i need the validation part</p> <p>any suggestion</p> 2008-09-25T18:02:54-04:002646522http://forums.asp.net/p/1325687/2646522.aspx/1?Re+xml+file+validation+with+xsdRe: xml file validation with xsd <p>Hi Csharp22, you need to do something like this:</p> <p>&nbsp;<pre class="prettyprint">/// &lt;summary&gt; /// Validates a xml against a xml schema. /// &lt;/summary&gt; public class XmlValidator { private bool _IsValid = true; /// &lt;summary&gt; /// Creates a new instance of XmlValidator. /// &lt;/summary&gt; /// &lt;param name=&quot;xml&quot;&gt;Path to xml file.&lt;/param&gt; /// &lt;param name=&quot;xsd&quot;&gt;Path to xml schema file.&lt;/param&gt; public XmlValidator(string xml, string xsd) { System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument(); xmlDoc.Load(xml); using (System.IO.FileStream fsStr = new System.IO.FileStream(xsd, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read)) { xmlDoc.Schemas.Add(System.Xml.Schema.XmlSchema.Read(fsStr, this.Validator)); } xmlDoc.Validate(this.Validator); } /// &lt;summary&gt; /// Indicates if the xml file has a valid schema. /// &lt;/summary&gt; public bool IsValid { get { return this._IsValid; } } private void Validator(object sender, System.Xml.Schema.ValidationEventArgs e) { //Check severity (if you need Warnings be treated as erros change this) if (e.Severity == System.Xml.Schema.XmlSeverityType.Error) { this._IsValid = false; } } }</pre> </p> <p>&nbsp;</p> <p>Bye,</p> <p>Miguel.</p> 2008-09-25T19:36:19-04:002646528http://forums.asp.net/p/1325687/2646528.aspx/1?Re+xml+file+validation+with+xsdRe: xml file validation with xsd <p>hi miguel,</p> <p>i wanted to do it in a stored procedure..</p> <p>&nbsp;</p> 2008-09-25T19:38:56-04:002646543http://forums.asp.net/p/1325687/2646543.aspx/1?Re+xml+file+validation+with+xsdRe: xml file validation with xsd <p>Where do you store the xml schema? In SQL Server? In a file?</p> <p>What version of Sql Server do you use? You need at least 2005 to&nbsp;use .NET assemblies inside it.</p> <p>Bye,</p> <p>Miguel.</p> 2008-09-25T19:47:44-04:002646602http://forums.asp.net/p/1325687/2646602.aspx/1?Re+xml+file+validation+with+xsdRe: xml file validation with xsd <p>hi,</p> <p>i am using sql server 2005.</p> <p>right now i have the schema in a file(.xsd) ....</p> <p>do i need to register the schema with sql server 2005 and then do validation</p> <p>&nbsp;</p> <p>any suggestion or sample example would be greatful</p> <p>&nbsp;</p> <p>tnx</p> 2008-09-25T20:18:10-04:002646710http://forums.asp.net/p/1325687/2646710.aspx/1?Re+xml+file+validation+with+xsdRe: xml file validation with xsd <p>Oks, the only way is to doing an assembly with visual studio 2005/2008 and embed in Sql Server 2005, then map the assembly procedures to stored procedures in order to use them inside a Sql Server Stored Procedure. Do you have any experience in Visual Studio (C# or Visual Basic.NET)?</p> <p>Bye,</p> <p>Miguel.</p> 2008-09-25T21:43:06-04:002646726http://forums.asp.net/p/1325687/2646726.aspx/1?Re+xml+file+validation+with+xsdRe: xml file validation with xsd <p>i dint do this before....</p> <p>it would be a great help if you let me know the steps for this.</p> <p>thx</p> 2008-09-25T21:55:06-04:002646736http://forums.asp.net/p/1325687/2646736.aspx/1?Re+xml+file+validation+with+xsdRe: xml file validation with xsd <p>Oks, you need to install a Visual Studio if you don't have yet. In <a href="http://www.microsoft.com/express/download/"> http://www.microsoft.com/express/download/</a>&nbsp;you can download and install it. Once you have installed it you have to create a new Poject (Class Library) in C# language. When you do those steps please post a new message and I guide you step by step to do it.</p> <p>Bye,</p> <p>Miguel.</p> 2008-09-25T22:02:14-04:002646748http://forums.asp.net/p/1325687/2646748.aspx/1?Re+xml+file+validation+with+xsdRe: xml file validation with xsd <p>hi,</p> <p>i have installed and created a new project...</p> <p>what next ?</p> <p>tnx</p> 2008-09-25T22:10:39-04:002646761http://forums.asp.net/p/1325687/2646761.aspx/1?Re+xml+file+validation+with+xsdRe: xml file validation with xsd <p>Oks, you are quick enough like Fernando Alonso [:P].</p> <p>The next step is to add a new class to the project (right click in the solution explorer on the project name and add new file, class). Name it XmlValidator.cs.</p> <p>How dou you named the project?</p> <p>&nbsp;</p> 2008-09-25T22:21:12-04:002646780http://forums.asp.net/p/1325687/2646780.aspx/1?Re+xml+file+validation+with+xsdRe: xml file validation with xsd <p>hi,</p> <p>i have given same name(XmlValidator) to both my project and class name</p> <p>&nbsp;</p> 2008-09-25T22:33:57-04:002646796http://forums.asp.net/p/1325687/2646796.aspx/1?Re+xml+file+validation+with+xsdRe: xml file validation with xsd <p>Oks... overwrite all the code auto generated of your class and paste the following:</p> <p>&nbsp;<pre class="prettyprint">using System; using System.Data.SqlClient; using System.Data.SqlTypes; using System.Text; using System.Xml; using System.Xml.Schema; using Microsoft.SqlServer.Server; namespace XmlValidator { public class XmlValidator { private bool IsValid = true; private XmlValidator(string xml, string xsd) { System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument(); xmlDoc.LoadXml(xml); using (System.IO.FileStream fsStr = new System.IO.FileStream(xsd, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read)) { xmlDoc.Schemas.Add(System.Xml.Schema.XmlSchema.Read(fsStr, this.Validator)); } xmlDoc.Validate(this.Validator); } private void Validator(object sender, System.Xml.Schema.ValidationEventArgs e) { //Check severity (if you need Warnings be treated as erros change this) if (e.Severity == System.Xml.Schema.XmlSeverityType.Error) { this.IsValid = false; } } [SqlFunction] public static SqlBoolean Validate(SqlXml xml, SqlString pathToXsd) { try { return new SqlBoolean(new XmlValidator(xml.Value, pathToXsd.Value).IsValid); } catch { } return new SqlBoolean(false); } } }</pre>&nbsp;&nbsp; </p> <p>Then build the project.</p> 2008-09-25T22:53:22-04:002646799http://forums.asp.net/p/1325687/2646799.aspx/1?Re+xml+file+validation+with+xsdRe: xml file validation with xsd <p>build succeeded!!</p> <p>&nbsp;</p> <p>[:)]</p> <p>&nbsp;</p> <p>&nbsp;</p> 2008-09-25T22:56:10-04:002646802http://forums.asp.net/p/1325687/2646802.aspx/1?Re+xml+file+validation+with+xsdRe: xml file validation with xsd <p>Well done!!!! [:)] iuuuuuuhu!! Oks the next step is to add the assembly to the Sql Server Database. Inside your database go to Programming-&gt;Assemblies and add the built assembly.</p> 2008-09-25T23:03:41-04:002646807http://forums.asp.net/p/1325687/2646807.aspx/1?Re+xml+file+validation+with+xsdRe: xml file validation with xsd <p>friend i have added the assembly...</p> <p>please let me know the other steps i will do it tomorror...its too late now time to go</p> <p>&nbsp;</p> <p>thank you very much fro the information...</p> <p>will test it tomm and let you know</p> <p>&nbsp;</p> <p>plz let me know how to validate from stored procedure </p> 2008-09-25T23:09:01-04:002646816http://forums.asp.net/p/1325687/2646816.aspx/1?Re+xml+file+validation+with+xsdRe: xml file validation with xsd <p>Oks... Run this script in your server:</p> <p>&nbsp;<pre class="prettyprint">USE YOUR_DATABASE GO EXEC sp_configure 'clr', 1 GO RECONFIGURE WITH OVERRIDE GO ALTER DATABASE [YOUR_DATABASE] SET TRUSTWORTHY ON GO CREATE FUNCTION ValidateXml (@Xml Xml, @PathToXsd NVARCHAR(255)) RETURNS BIT AS EXTERNAL NAME XmlValidator.[XmlValidator.XmlValidator].Validate GO</pre> <P mce_keep="true">&nbsp;</P> <P>And to test it... run a new query in your database:</P><pre class="prettyprint"><SPAN class=sqlkwd>SELECT</SPAN> [dbo].[ValidateXml](<SPAN class=st>'&lt;xml&gt;&lt;/xml&gt;'</SPAN>, <SPAN class=st>'PATH_OF_THE_XSD'</SPAN>) </pre>&nbsp;&nbsp; </p> <p>If dont work we can test it tomorrow.</p> <p>Bye,</p> <p>Miguel</p> 2008-09-25T23:21:24-04:002646822http://forums.asp.net/p/1325687/2646822.aspx/1?Re+xml+file+validation+with+xsdRe: xml file validation with xsd <p>Hi! I forgot tell you when you add the assembly to the sql server you must enable &quot;External access&quot; in the permissions.</p> <p>Bye,</p> <p>Miguel.</p> 2008-09-25T23:32:37-04:002648773http://forums.asp.net/p/1325687/2648773.aspx/1?Re+xml+file+validation+with+xsdRe: xml file validation with xsd <p>hi,</p> <p>when i try to enable 'External access' i get the following error</p> <span style="font-size:10pt; color:blue; font-family:'Courier New'">Create Assembly for assembly XmlValidator failed because assembly XmlValidator is not authorized for PERMISSION_SET = EXTERNAL_ACCESS. The assembly is authorized when either of the following is true;</span> <p class="MsoNormal" style="margin:0in 0in 0pt"><span style="font-size:10pt; color:blue; font-family:'Courier New'">The database owner(DBO) has EXTERNAL ACCESS ASSEMBLY permission and the database has the TRUSTWORTY database property on; or the assembly is signed with as certificate or an asymmetric key that has a corresponding login with EXTERNAL ACCESS ASSEMBLY permission.(Microsoft SQL Server , Error: 10327)</span></p> <p>any suggestion</p> <p>&nbsp;</p> <p>tnx</p> 2008-09-26T16:41:43-04:002648809http://forums.asp.net/p/1325687/2648809.aspx/1?Re+xml+file+validation+with+xsdRe: xml file validation with xsd <p>when i have enabled Trustworty property it went through</p> <p>&nbsp;</p> <p>&nbsp;</p> 2008-09-26T16:58:51-04:002649169http://forums.asp.net/p/1325687/2649169.aspx/1?Re+xml+file+validation+with+xsdRe: xml file validation with xsd <p>Hi, is the solution&nbsp;working?</p> <p>Bye, Miguel.</p> 2008-09-26T20:12:39-04:00