Click here to Skip to main content
15,891,720 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have given my code below
C#
public static void XMLSchema()
       {
           string pathToXmlFile = @"..\..\XML\Sample.xml";
           string pathOfXmlSchemaFile = @"..\..\Sample.xsd";
           try
           {
               XmlReaderSettings xmlSettings = new XmlReaderSettings();
               xmlSettings.Schemas = new System.Xml.Schema.XmlSchemaSet();
               using (var stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(pathOfXmlSchemaFile))
               {
                   //Log.WriteLog("ValidateXmlFileElement", "ValidateXmlFileElement started");
               XmlReader xmlReader = XmlReader.Create(pathOfXmlSchemaFile);
                   xmlSettings.Schemas.Add("", xmlReader);

               }
               xmlSettings.ValidationType = ValidationType.Schema;

               XmlReader reader = XmlReader.Create(pathToXmlFile, xmlSettings);
               while (reader.Read()) ;
               Console.WriteLine("success");
           }
           catch (Exception ex)
           {
               //Log.WriteLog("ValidateXmlFileElement", "ValidateXmlFileElement completed");
               string errorMessage = string.Format("{0}{1}", "\t", ex.Message.ToString(), ex.InnerException);
               Console.WriteLine(errorMessage);
               //Log.WriteLog("ValidateXmlFileElement", errorMessage);
           }



if i run this code using windows scheduler or task scheduler, so i am getting error in this line which is given below
C#
var stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(pathOfXmlSchemaFile

here stream value is null,

even i set xsd file properties like build action=embeded resource in vs2010

Can anyone help me how to getrid of from this issue, thanks in advance


Thanks
Yash
Posted
Updated 10-Dec-14 9:20am
v2

1 solution

The GetManifestResourceStream function[^] is used to read a resource embedded in your assembly. You are passing a relative path to a file on disk. That will never work, regardless of the project type.

If you want to read an embedded resource, you need to pass a valid resource name to the GetManifestResourceStream function:
C#
using (var stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("Namespace.Of.Your.Resource.Sample.xsd"))
{
    ...
}


If you want to read the file from disk, then use the correct method:
C#
using (var stream = System.IO.File.OpenRead(@"..\..\Sample.xsd"))
{
    ...
}
 
Share this answer
 
Comments
Maciej Los 10-Dec-14 15:21pm    
+5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900