Validating XML Using XSD file in MVC 3 C#
In this tutorial we will see how to validate Xml file using Xsd. We have kept the Xml and Xsd files in the solution itself.
Xml File :
<?xml version="1.0"?>
<books>
<book id="bk001">
<author>Writer</author>
<title>The First Book</title>
<genre>Fiction</genre>
<price>44.95</price>
<pub_date>2000-10-01</pub_date>
<review>An amazing story of nothing.</review>
</book>
<book id="bk002">
<author>Poet</author>
<title>The Poet's First Poem</title>
<genre>Poem</genre>
<price>28.32</price>
<review>Least poetic poems.</review>
</book>
</books>
The above is simple xml file. It has books as root element.
XSD File :
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" attributeFormDefault="unqualified" elementFormDefault="qualified">
<xs:element name="books">
<xs:complexType>
<xs:sequence>
<xs:element name="book" maxOccurs="unbounded" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:string" name="author"/>
<xs:element type="xs:string" name="title"/>
<xs:element type="xs:string" name="genre"/>
<xs:element type="xs:float" name="price"/>
<xs:element type="xs:date" name="pub_date" minOccurs="0"/>
<xs:element type="xs:string" name="review"/>
</xs:sequence>
<xs:attribute type="xs:string" name="id" use="optional"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
The above is the Xsd file of books.xml. We have applied datatype constraints in the xsd for the values of the xml elements.
In Controller :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ValidateXmlUsingXsd.Service;
namespace ValidateXmlUsingXsd.Controllers
{
public class HomeController : Controller
{
//
// GET: /Home/
public ActionResult Index()
{
Validations validate = new Validations();
string isValidate = validate.ValidateXMlUsingXSD();
if (isValidate == string.Empty)
isValidate = "Xml is well Formed according to XSD.";
ViewBag.Validate = isValidate;
return View();
}
}
}
We have the basic Home controller, with Index ActionResult method. In the Index method, we have created an object of Validations class, and using the object we have called ValidateXMLUsingXSD() method. This method validates the Xml file against the Xsd file. Finally the result is added to ViewBag and View is rendered.
In Service Class - Validations.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;
using System.Xml.Schema;
using System.Text;
namespace ValidateXmlUsingXsd.Service
{
public class Validations
{
public string ValidateXMlUsingXSD()
{
try
{
string xmlPath = @"c:\users\documents\visual studio 2010\Projects\ValidateXmlUsingXsd\ValidateXmlUsingXsd\XML\books.xml";
string xsdPath = @"c:\users\documents\visual studio 2010\Projects\ValidateXmlUsingXsd\ValidateXmlUsingXsd\XSD\books.xsd";
XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidationType = ValidationType.Schema;
settings.Schemas.Add(null, XmlReader.Create(xsdPath));
XmlReader xmlReader = XmlReader.Create(xmlPath, settings);
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(xmlReader);
return string.Empty;
}
catch (Exception e)
{
return e.Message;
}
}
}
}
The ValidateXMLUsingXSD() method validates the Xml file against Xsd file. In this method, we have declared two string variables to hold path of Xml and Xsd file respectively. We create object of XmlReaderSettings class. We set the ValidationType to schema for this object. The schema file is read from the specified path using XmlReader class's create method. Similarly, while reading Xml file, settings is also path. When the read Xml file is loaded to the XmlDocument, the XmlReader object is passed which contain the XmlReaderSettings as well. So Xml file is validated against Xsd while loading. When validation fails, exception is caught in catch block and exception message is returned.
In View :
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>Is XMl Successfuly Validated ?? : @ViewBag.Validate</p>
In View, we are displaying the validation result, which is passed to the view using ViewBag from the controller.
So, this tutorial shows how to validate Xml using Xsd using simple example.
Video :
0 comments:
Post a Comment