简体   繁体   中英

System.IO.DirectoryNotFoundException: When validating XML against a XSD

I have a program written in C# that validates a XML files against a schema.

Sometimes when I call xmlSchemaCollection.Add(null, schemaFileName) where xmlSchemaCollection is a XmlSchemaCollection and schemaFileName is the uri to the schema file I get:

“System.IO.DirectoryNotFoundException: Could not find a part of the path”.

The file with the schema exists on a local disc and everyone has read permission on it. This happens randomly about one time out of 100.

Has anyone seen this before?

Your schema references another schema and the parser is trying to open it from an (unexisting) file location. Can you post the first lines of the xsd?

Write like this

sc.Add(null, "DataSet1.xsd");  

Add DataSet1.xsd to your project and set property "Copy to Output Directory" to "Copy always". (Right click DataSet1.xsd and select properties to set the property)

Hope this will help you...

Edited

I have created the same path and same schema name in my system that you have given and I am able to execute following code without any exception:

try
        {
            XmlSchemaCollection sc = new XmlSchemaCollection();
            sc.ValidationEventHandler += new ValidationEventHandler(sc_ValidationEventHandler);

            var schemaFileName = "C:\\BrackeGis\\xmlschema\\BGO-Info-1_2.xsd";

            sc.Add(null, schemaFileName);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex);
        }  

There must be some mistake in path that you have created . . .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM