{"id":3485,"date":"2012-03-14T15:55:29","date_gmt":"2012-03-14T13:55:29","guid":{"rendered":"https:\/\/ingmarverheij.com\/?p=3485"},"modified":"2012-03-14T16:26:22","modified_gmt":"2012-03-14T14:26:22","slug":"dataset-readxml-throws-directorynotfoundexception","status":"publish","type":"post","link":"https:\/\/ingmarverheij.com\/en\/dataset-readxml-throws-directorynotfoundexception\/","title":{"rendered":"DataSet.ReadXML throws DirectoryNotFoundException"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; border-right-width: 0px; margin: 0px 0px 0px 5px; padding-left: 0px; padding-right: 0px; display: inline; float: right; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px\" title=\"\" border=\"0\" alt=\"\" align=\"right\" src=\"https:\/\/ingmarverheij.com\/wp-content\/uploads\/2012\/03\/xmlLogo.jpg\" width=\"119\" height=\"73\" \/>A .NET application that reads a dataset from an XML using the <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/system.data.dataset.readxml.aspx\" target=\"_blank\">DataSet.ReadXML<\/a> method might throw the exception : <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/system.io.directorynotfoundexception.aspx\" target=\"_blank\"><font face=\"Courier New\">System.IO.DirectoryNotFoundException<\/font><\/a><font face=\"Courier New\">: Could not find a part of the path &#8216;&lt;path of XML file&gt;&#8217;.<\/font><\/p>\n<p><a href=\"https:\/\/ingmarverheij.com\/wp-content\/uploads\/2012\/03\/DirectoryNotFoundException.png\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; border-right-width: 0px; margin: 0px 5px 0px 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px\" title=\"\" border=\"0\" alt=\"\" src=\"https:\/\/ingmarverheij.com\/wp-content\/uploads\/2012\/03\/DirectoryNotFoundException_thumb.png\" width=\"593\" height=\"76\" \/><\/a><\/p>\n<p>According to MSDN this exception is thrown when when \u201cpart of a file or directory cannot be found\u201d .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 <strong>invalid<\/strong> (for instance because you didn\u2019t close a node)<\/p>\n<p><!--more--><\/p>\n<h4>Example<\/h4>\n<p><strong>Good structure<\/strong><\/p>\n<pre>&lt;books&gt;\n   &lt;title&gt;Xml For Dummies&lt;\/title&gt;\n   &lt;author&gt;Lucinda Dykes, Ed Tittel&lt;\/author&gt;\n   &lt;URL&gt;<a title=\"https:\/\/books.google.nl\/books\/about\/Xml_For_Dummies.html?id=T0zQVjBTlzAC&amp;#38;redir_esc=y\" href=\"https:\/\/books.google.nl\/books\/about\/Xml_For_Dummies.html?id=T0zQVjBTlzAC&amp;#38;redir_esc=y\">https:\/\/books.google.nl\/books\/about\/Xml_For_Dummies.html?id=T0zQVjBTlzAC&amp;#38;redir_esc=y<\/a>&lt;\/URL&gt;\n&lt;\/books&gt;<\/pre>\n<p><strong>Bad structure<\/strong><\/p>\n<pre>&lt;books&gt;\n   &lt;title&gt;Xml For Dummies&lt;\/title&gt;\n   &lt;author&gt;Lucinda Dykes, Ed Tittel   &lt;\u2014 something is missing here\n   &lt;URL&gt;<a title=\"https:\/\/books.google.nl\/books\/about\/Xml_For_Dummies.html?id=T0zQVjBTlzAC&amp;#38;redir_esc=y\" href=\"https:\/\/books.google.nl\/books\/about\/Xml_For_Dummies.html?id=T0zQVjBTlzAC&amp;#38;redir_esc=y\">https:\/\/books.google.nl\/books\/about\/Xml_For_Dummies.html?id=T0zQVjBTlzAC&amp;#38;redir_esc=y<\/a>&lt;\/URL&gt;\n&lt;\/books&gt;<\/pre>\n<p>&#160;<\/p>\n<h4>Validate in code<\/h4>\n<p>Before you read an XML file using the <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/system.data.dataset.readxml.aspx\" target=\"_blank\">DataSet.ReadXML<\/a> method, always validate the XML file (against a schema). This can easily be done by first reading the XML file with the <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/system.xml.xmlreadersettings.validationeventhandler.aspx#Y337\" target=\"_blank\">XmlReaderSettings<\/a> or&#160; <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/system.xml.xmlvalidatingreader.aspx\" target=\"_blank\">XmlValidatingReader<\/a> class. By validating the XML file you\u2019re always sure the structure of the file is correct and the ReadXML method won\u2019t raise a bogus event. <\/p>\n<p>Microsoft has supplied us with some examples <a href=\"https:\/\/support.microsoft.com\/kb\/317463\" target=\"_blank\">here<\/a> and <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/system.xml.xmlreadersettings.validationeventhandler.aspx#Y337\" target=\"_blank\">here<\/a>.<\/p>\n<p>&#160;<\/p>\n<h4>Validate manually<\/h4>\n<p>You can validate your XML file manually by opening the XML file in Internet Explorer. If the file structured correct you\u2019\u2019ll be able to expand and collapse the nodes.<\/p>\n<p><a href=\"https:\/\/ingmarverheij.com\/wp-content\/uploads\/2012\/03\/IE.png\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; border-bottom: 0px; border-left: 0px; margin: 0px 5px 0px 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px\" title=\"\" border=\"0\" alt=\"\" src=\"https:\/\/ingmarverheij.com\/wp-content\/uploads\/2012\/03\/IE_thumb.png\" width=\"604\" height=\"129\" \/><\/a><\/p>","protected":false},"excerpt":{"rendered":"<p>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 &#8216;&lt;path of XML file&gt;&#8217;. According to MSDN this exception is thrown when when \u201cpart of a file or directory cannot be found\u201d .However, this exception is not [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"site-container-style":"default","site-container-layout":"default","site-sidebar-layout":"default","disable-article-header":"default","disable-site-header":"default","disable-site-footer":"default","disable-content-area-spacing":"default","footnotes":""},"categories":[312],"tags":[426,239,241],"class_list":["post-3485","post","type-post","status-publish","format-standard","hentry","category-vb-net-scripting","tag-directorynotfoundexception","tag-readxml","tag-vb-net"],"_links":{"self":[{"href":"https:\/\/ingmarverheij.com\/en\/wp-json\/wp\/v2\/posts\/3485","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ingmarverheij.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ingmarverheij.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ingmarverheij.com\/en\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/ingmarverheij.com\/en\/wp-json\/wp\/v2\/comments?post=3485"}],"version-history":[{"count":6,"href":"https:\/\/ingmarverheij.com\/en\/wp-json\/wp\/v2\/posts\/3485\/revisions"}],"predecessor-version":[{"id":3496,"href":"https:\/\/ingmarverheij.com\/en\/wp-json\/wp\/v2\/posts\/3485\/revisions\/3496"}],"wp:attachment":[{"href":"https:\/\/ingmarverheij.com\/en\/wp-json\/wp\/v2\/media?parent=3485"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ingmarverheij.com\/en\/wp-json\/wp\/v2\/categories?post=3485"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ingmarverheij.com\/en\/wp-json\/wp\/v2\/tags?post=3485"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}