Tagged: Xml

Validating XML using XMLValidatingReader

Level: Beginner

You can use XmlValidatingReader class to validate an cml file against an XSD schema, Here is the code snippet to do it,

XmlSchemaCollection schemaCol = new XmlSchemaCollection();
XmlTextReader textReader = null;
XmlValidatingReader vReader = null;
textReader = new XmlTextReader(xmlFile);
// create a validating reader.
vReader = new XmlValidatingReader(textReader);
// validate using the schemas stored in the schema collection.
vReader.Schemas.Add (schemaCol);
// set the validation event handler
vReader.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack);
// read and validate the XML data.
while (vReader.Read()){}
catch (Exception ex)
return (false);
// close the readers

vReader.Close ();
textReader.Close ();


And your Event Handler to handle the validation callbacks,

private void ValidationCallBack (object sender, ValidationEventArgs args)
this._xmlFeedValid = false;

Access Denied Error with MSXML 4.0 SP2

Level: Advanced

If you have recently updated your MSXML 4.0 with SP2 and started getting an “Access Denied” error, then read on…
According to this Microsoft article, Security in the implementation of the MSXML 4.0 SP2 ServerXmlHttp object has been enhanced to check the Internet Explorer security policy setting for submitting non-encrypted form data.

Follow these steps to configure IE Security,

1. Click Start, click Run, type mmc, and then press ENTER.
2. On the File or the Action menu, click Add/Remove Snap-in.
3. In the Add/Remove Snap-in dialog box, click Add.
4. On the Standalone tab, click Add. In the Available Standalone Snap-in dialog box, click Group Policy , and then click Add. The Group Policy Wizard appears.
5. In the Group Policy Wizard, Click Finish
6. Close the Add Standalone Snap-in window by clicking the Close button
7. Click OK in the Add/Remove Snap-in dialog box.
8. Under User Configuration, expand Windows Settings, expand Internet Explorer Maintenance, and then click Security.
9. In the right pane, double-click Security Zones and Content Ratings.
10. Under Security Zones and Privacy, click Import the current security zones and privacy settings, and then click Modify Settings.
11. Select the zone that you would like to modify, and click Custom Level
12. Modify the settings to enable the Submit nonencrypted form data option by selecting the enable radio button for that option. If it is already enabled, then just click the OK button. The zone where the setting should be enabled is determined by the zone where the target URL of the POST operation is classified. For example, when you post to an Internet URL, you must enable this option for the internet zone.
13. Restart the process that is running ServerXMLHTTP. To do this, you may have to restart your computer.

Creating DataSet from XML and XSD

Level: Beginner-Intermediate

If you want to read your XML file into a DataSet, you could use the ReadXml method of the DataSet. You can read the XSD for the same XML file using ReadXMLSchema method. Here is how to use these methods,

string doc = @”c:\test.xml”;
string docSchema = @”c:\test.xsd”;
XmlDataDocument myXmlDataDocument = new XmlDataDocument ;

We will use a StreamReader to read the schema file,

StreamReader reader = new StreamReader(docSchema);
//this will read the schema to DataSet

[do not forget to close the StreammReader in your actual code]

//this will read the XML

DataSet also has a ReadXML method you can use to read the XML. This method can be used instead of XMLDataDocument’s Load method. ReadXML method takes a second parameter that specifies XMLReadMode [enumeration].
Read more about this on MSDN:

All these methods works well with simple XML and XSD. In my experience, if you have many complex types and relations in XML/XSD, ReadXMLSchema method gives undefined error! on some types [native and sometimes derived!] and subsequently Load fails!!