Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Oct 08, 2008 07:20 PM by Csharp22
Sep 25, 2008 06:02 PM|LINK
i am inserting data from xml into sql 2005 table.
now before reading xml file i want to validate the xml file with the schema.
if validated i want to open and do my operation.
else i want to log the error.
i am able to insert the xml data into database table(with openxml) but i need the validation part
Sep 25, 2008 07:36 PM|LINK
Hi Csharp22, you need to do something like this:
/// Validates a xml against a xml schema.
public class XmlValidator
private bool _IsValid = true;
/// Creates a new instance of XmlValidator.
/// <param name="xml">Path to xml file.</param>
/// <param name="xsd">Path to xml schema file.</param>
public XmlValidator(string xml, string xsd)
System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
using (System.IO.FileStream fsStr = new System.IO.FileStream(xsd, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read))
/// Indicates if the xml file has a valid schema.
public bool 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;
Sep 25, 2008 07:38 PM|LINK
i wanted to do it in a stored procedure..
Sep 25, 2008 07:47 PM|LINK
Where do you store the xml schema? In SQL Server? In a file?
What version of Sql Server do you use? You need at least 2005 to use .NET assemblies inside it.
Sep 25, 2008 08:18 PM|LINK
i am using sql server 2005.
right now i have the schema in a file(.xsd) ....
do i need to register the schema with sql server 2005 and then do validation
any suggestion or sample example would be greatful
Sep 25, 2008 09:43 PM|LINK
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)?
Sep 25, 2008 09:55 PM|LINK
i dint do this before....
it would be a great help if you let me know the steps for this.
Sep 25, 2008 10:02 PM|LINK
Oks, you need to install a Visual Studio if you don't have yet. In
http://www.microsoft.com/express/download/ 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.
Sep 25, 2008 10:10 PM|LINK
i have installed and created a new project...
what next ?
Sep 25, 2008 10:21 PM|LINK
Oks, you are quick enough like Fernando Alonso [: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.
How dou you named the project?