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.

Example:

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()

{

//Headlines

ReadingXML(“https://ujjwaladatta.wordpress.com/feed/feeds.xml”, “D:\\news\\blog.xml”);

}

public void ReadingXML(string GlobalUrl, string LocalUrl)

{

try

{

reader = new XmlTextReader(GlobalUrl);

DataSet ds = new DataSet();

ds.ReadXml(reader);

{

valid = new XmlValidatingReader(reader);

valid.ValidationType = ValidationType.DTD;

valid.ValidationEventHandler += new ValidationEventHandler(MyValidationEventHandler);

if (issValid)

{

WebClient client = new WebClient();

client.DownloadFile(GlobalUrl, LocalUrl);

}

}

reader.Close();

}

catch (Exception ex)

{

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

}

}

public static void MyValidationEventHandler(object sender, ValidationEventArgs args)

{

issValid = false;

}

}

}

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s