DataSet.ReadXML throws DirectoryNotFoundException

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>https://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>https://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.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

en_USEnglish