Validating an XML Document by Using DTD, XDR, or XSD in C# .NET

This article demonstrates how to use the XmlValidatingReader class to validate an XML document against the specified grammar.


using System.Xml.Schema;

using System.Xml;

using System.Net;

namespace RssNewsDownload


class RssNews


public static bool issValid = true;

public XmlTextReader reader;

public XmlValidatingReader valid;

public void RssLinks()



ReadingXML(“”, “D:\\news\\blog.xml”);


public void ReadingXML(string GlobalUrl, string LocalUrl)




reader = new XmlTextReader(GlobalUrl);

DataSet ds = new DataSet();



valid = new XmlValidatingReader(reader);

valid.ValidationType = ValidationType.DTD;

valid.ValidationEventHandler += new ValidationEventHandler(MyValidationEventHandler);

if (issValid)


WebClient client = new WebClient();

client.DownloadFile(GlobalUrl, LocalUrl);





catch (Exception ex)


SaveErrorTOLog(ex, Session[“USER”].ToString());



public static void MyValidationEventHandler(object sender, ValidationEventArgs args)


issValid = false;





Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s