How would you validate XML using .NET?

Posted by Saranya Boopathi under ASP.NET on 6/11/2013 | Points: 10 | Views : 620 | Status : [Member] | Replies : 2
How would you validate XML using ASP.NET?

Saranya Boopathi


Responses

Posted by: Satyapriyanayak on: 6/11/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
http://www.codeproject.com/Articles/10444/Simple-code-to-validate-an-XML-file-against-a-sche
http://www.developerfusion.com/code/2518/how-to-validate-an-xml-file-with-an-xsd-file/
http://forums.asp.net/t/1511997.aspx/1

If this post helps you mark it as answer
Thanks

Saranya Boopathi, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Allemahesh on: 6/14/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
You can use the bellow code files to solve you problem.

validate.aspx.cs

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Xml;
using System.Xml.Schema;

namespace validate
{
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.Label Label3;
protected System.Web.UI.WebControls.Label TheXml;
protected System.Web.UI.WebControls.Label Label2;
protected System.Web.UI.WebControls.Label Output;
protected System.Web.UI.WebControls.Label Outcome;
protected System.Web.UI.WebControls.Label TheXsd;

private void Page_Load(object sender, System.EventArgs e)
{
//define variables
TheXml.Text = HttpContext.Current.Server.MapPath("books.xml");
TheXsd.Text = HttpContext.Current.Server.MapPath("books.xsd");
Outcome.Text = "";
}

private void Button1_Click(object sender, System.EventArgs e)
{
//define variables
Outcome.Text = "<font color=\"green\">Succeeded</font>";
Output.Text = "";

//load schema
XmlSchemaCollection xsc = new XmlSchemaCollection();
xsc.Add("generic", TheXsd.Text);
Validate(TheXml.Text, xsc);
}

private void Validate(String filename, XmlSchemaCollection xsc)
{
XmlTextReader reader = null;
XmlValidatingReader vreader = null;

reader = new XmlTextReader (filename);
vreader = new XmlValidatingReader (reader);
vreader.Schemas.Add(xsc);
vreader.ValidationEventHandler += new ValidationEventHandler (ValidationCallBack);
try
{
while (vreader.Read()){}
}
catch
{
Output.Text = "XML Document is not well-formed.";
}
vreader.Close();
}

public void ValidationCallBack (object sender, ValidationEventArgs args)
{
Outcome.Text = "<font color=\"red\">Failed:</font>";
Output.Text += "Validation error: <font color=\"red\">" + args.Message + "</font><br>";
}
}
}

Books.xml

<?xml version="1.0"?>
<bookstore xmlns="generic">
<book genre="autobiography">
<title>The Autobiography of Benjamin Franklin</title>
<author>
<first-name>Ben</first-name>
<last-name>Franklin</last-name>
</author>
<price>89.88</price>
</book>
<book genre="novel">
<title>The Confidence Man</title>
<author>
<first-name>John</first-name>
<last-name>Melville</last-name>
</author>
<price>11.99</price>
</book>
</bookstore>



Books.xsd
---------------------------
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="generic" elementFormDefault="qualified" targetNamespace="generic">
<xsd:element name="bookstore" type="bookstoreType"/>
<xsd:complexType name="bookstoreType">
<xsd:sequence maxOccurs="unbounded">
<xsd:element name="book" type="bookType"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="bookType">
<xsd:sequence>
<xsd:element name="title" type="xsd:string"/>
<xsd:element name="author" type="authorName"/>
<xsd:element name="price" type="xsd:decimal"/>
</xsd:sequence>
<xsd:attribute name="genre" type="xsd:string"/>
</xsd:complexType>
<xsd:complexType name="authorName">
<xsd:sequence>
<xsd:element name="first-name" type="xsd:string"/>
<xsd:element name="last-name" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>

Hope this will help to solve your problem.

Saranya Boopathi, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response