DataSet.ReadXML throws DirectoryNotFoundException

Author: Ingmar Verheij

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

<books>
   <title>Xml For Dummies</title>
   <author>Lucinda Dykes, Ed Tittel</author>
   <URL>http://books.google.nl/books/about/Xml_For_Dummies.html?id=T0zQVjBTlzAC&#38;redir_esc=y</URL>
</books>

Bad structure

<books>
   <title>Xml For Dummies</title>
   <author>Lucinda Dykes, Ed Tittel   <— something is missing here
   <URL>http://books.google.nl/books/about/Xml_For_Dummies.html?id=T0zQVjBTlzAC&#38;redir_esc=y</URL>
</books>

 

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.

IE_thumb[1]