/// <summary>
/// Validates a xml against a xml schema.
/// </summary>
public class XmlValidator
{
private bool _IsValid = true;
/// <summary>
/// Creates a new instance of XmlValidator.
/// </summary>
/// <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();
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);
}
/// <summary>
/// Indicates if the xml file has a valid schema.
/// </summary>
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;
}
}
}
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)?
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.
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.
Csharp22
Member
296 Points
410 Posts
xml file validation with xsd
Sep 25, 2008 06:02 PM|LINK
hi,
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.
any suggestions...
i am able to insert the xml data into database table(with openxml) but i need the validation part
any suggestion
mfcorral
Member
240 Points
50 Posts
Re: xml file validation with xsd
Sep 25, 2008 07:36 PM|LINK
Hi Csharp22, you need to do something like this:
/// <summary> /// Validates a xml against a xml schema. /// </summary> public class XmlValidator { private bool _IsValid = true; /// <summary> /// Creates a new instance of XmlValidator. /// </summary> /// <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(); 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); } /// <summary> /// Indicates if the xml file has a valid schema. /// </summary> 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; } } }Bye,
Miguel.
Csharp22
Member
296 Points
410 Posts
Re: xml file validation with xsd
Sep 25, 2008 07:38 PM|LINK
hi miguel,
i wanted to do it in a stored procedure..
mfcorral
Member
240 Points
50 Posts
Re: xml file validation with xsd
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.
Bye,
Miguel.
Csharp22
Member
296 Points
410 Posts
Re: xml file validation with xsd
Sep 25, 2008 08:18 PM|LINK
hi,
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
tnx
mfcorral
Member
240 Points
50 Posts
Re: xml file validation with xsd
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)?
Bye,
Miguel.
Csharp22
Member
296 Points
410 Posts
Re: xml file validation with xsd
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.
thx
mfcorral
Member
240 Points
50 Posts
Re: xml file validation with xsd
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.
Bye,
Miguel.
Csharp22
Member
296 Points
410 Posts
Re: xml file validation with xsd
Sep 25, 2008 10:10 PM|LINK
hi,
i have installed and created a new project...
what next ?
tnx
mfcorral
Member
240 Points
50 Posts
Re: xml file validation with xsd
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?