DataSet.ReadXML throws DirectoryNotFoundException

Written by Ingmar Verheij on March 14th, 2012. Posted in VB.Net

A .NET application that reads a dataset from an XML using the DataSet.ReadXML method might throw the exception : System.IO.DirectoryNotFoundException: Could not find a part of the path ‘<path of XML file>’.

According to MSDN this exception is thrown when when “part of a file or directory cannot be found” .However, this exception is not only thrown when a file cannot be found on the disk. The exception is also thrown when the structure of the XML is invalid (for instance because you didn’t close a node)

Example

Good structure

Bad structure

 

Validate in code

Before you read an XML file using the DataSet.ReadXML method, always validate the XML file (against a schema). This can easily be done by first reading the XML file with the XmlReaderSettings or  XmlValidatingReader class. By validating the XML file you’re always sure the structure of the file is correct and the ReadXML method won’t raise a bogus event.

Microsoft has supplied us with some examples here and here.

 

Validate manually

You can validate your XML file manually by opening the XML file in Internet Explorer. If the file structured correct you’’ll be able to expand and collapse the nodes.

Ingmar Verheij

At the time Ingmar wrote this article he worked for PepperByte as a Senior Consultant (up to May 2014). His work consisted of designing, migrating and troubleshooting Microsoft and Citrix infrastructures. He was working with technologies like Microsoft RDS, user environment management and (performance) monitoring. Ingmar is User Group leader of the Dutch Citrix User Group (DuCUG). RES Software named Ingmar RES Software Valued Professional in 2014.

More Posts - Website

Follow Me:
TwitterLinkedInGoogle Plus

Tags: , ,

Leave a comment

*

Donate

%d bloggers like this: