无法使用Windows服务或任务计划程序读取xsd路径 [英] UNable to read xsd path using windows service or task scheduler

查看:78
本文介绍了无法使用Windows服务或任务计划程序读取xsd路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已在下面提供了我的代码

  public   static   void  XMLSchema()
{
string pathToXmlFile = < span class =code-string> @ .. \..\XML \Sample.xml;
string pathOfXmlSchemaFile = @ .. \\ \\..\Sample.xsd;
try
{
XmlReaderSettings xmlSettings = new XmlReaderSettings();
xmlSettings.Schemas = new System.Xml.Schema.XmlSchemaSet();
使用 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 (例外情况)
{
// Log.WriteLog(ValidateXmlFileElement,ValidateXmlFileElement completed);
string errorMessage = string .Format( {0} {1 } \t,ex.Message.ToString(),ex .InnerException);
Console.WriteLine(errorMessage);
// Log.WriteLog(ValidateXmlFileElement,errorMessage);
}







如果我使用Windows调度程序或任务调度程序运行此代码,所以我是在下面给出的这一行中得到错误

  var  stream = System.Reflection.Assembly.GetExecutingAssembly( ).GetManifestResourceStream(pathOfXmlSchemaFile 



这里的流值为null,



即使我设置了像build这样的xsd文件属性动作=在vs2010中嵌入资源



任何人都可以帮我解决这个问题,请提前谢谢





谢谢

Yash

解决方案

GetManifestR esourceStream function [ ^ ]用于读取程序集中嵌入的资源。您正在将相对路径传递给磁盘上的文件。无论项目类型如何,这都将无法工作。



如果您想要读取嵌入式资源,则需要将有效的资源名称传递给 GetManifestResourceStream 功能:

 使用 var  stream = System.Reflection.Assembly.GetExecutingAssembly()。GetManifestResourceStream( 命名空间。 Of.Your.Resource.Sample.xsd))
{
...
}





如果你想从磁盘读取文件,那么使用正确的方法:

 使用 var  stream = System.IO.File.OpenRead( @  .. \..\Sample.xsd))
{
...
}


I have given my code below

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

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

解决方案

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:

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:

using (var stream = System.IO.File.OpenRead(@"..\..\Sample.xsd"))
{
    ...
}


这篇关于无法使用Windows服务或任务计划程序读取xsd路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆