2011-02-07 54 views
1

我試圖從XML文件中加載Spring.NET上下文。我有以下代碼:Spring.NET XML配置困境

public class ApplicationContextFactory 
{ 
    private static IApplicationContext _context; 
    public static IApplicationContext GetContext() 
    { 
     if (_context == null) 
     { 
      try 
      { 
       string data = new StreamReader(
         Assembly.GetExecutingAssembly(). 
         GetManifestResourceStream("Nmspace.Fldr.spring-config.xml")) 
         .ReadToEnd(); 
       using (var temp = File.CreateText("ctx.xml")) 
        temp.WriteLine(data); 
       _context = new XmlApplicationContext("ctx.xml"); 
       // _context = new XmlApplicationContext(
        //"assembly://DataLoader/DataLoader/Config.spring-config.xml"); 
      } 
      catch (Exception e) 
      { 
       string error = e.Message; 
      } 
     } 
     return _context; 
    } 
} 

我收到以下異常:

行25 XML文檔中的文件,從違反[D:\correct\path\to\ctx.xml]的模式。 'http://www.springframework.net/database:provider'元素未被聲明。

如果我直接從程序集中拉出,則會出現相同的錯誤。 (註釋掉了行)

真奇怪的是,在我開始一個新項目並嘗試在我的新項目中使用配置之前,我沒有任何問題。 (此代碼和配置文件已經工作了幾個月的老項目,仍然沒有。)

編輯:

xmlns聲明:

<objects 
    xmlns="http://www.springframework.net" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:tx="http://www.springframework.net/tx" 
    xmlns:db="http://www.springframework.net/database" 
    xmlns:aop="http://www.springframework.net/aop" 
    xsi:schemaLocation="http://www.springframework.net http://www.springframework.net/schema/objects/spring-objects.xsd 
     http://www.springframework.net/schema/tx http://www.springframework.net/schema/tx/spring-tx-1.1.xsd 
     http://www.springframework.net/schema/db http://www.springframework.net/schema/db/spring-database.xsd 
     http://www.springframework.net/aop http://www.springframework.net/schema/aop/spring-aop-1.1.xsd" 
> 

的問題行(25):

<db:provider 
    id="localDbProvider" 
    provider="OracleClient-2.0" 
    connectionString= 
     "Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=localhost)(PORT=1521))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME = xe))); User Id=cmdb; Password=password;"/> 
+0

你可以添加「ctx.xml」的第25行嗎? – Marijn 2011-02-07 19:36:06

+0

@Marijn補充說。希望有所幫助。 – jjnguy 2011-02-07 19:49:12

回答

0

聽起來像'知名命名空間解析器'的發現和註冊出了問題。要解決這個問題,嘗試聲明XML文件本身命名空間中的別名,如...

<objects xmlns='http://www.springframework.net' 
    xmlns:database="http://www.springframework.net/database"> 
    ... 
</objects> 

...然後看看是否能正常工作。你使用的是什麼版本的Spring.NET?並且它與您以前的項目中使用的版本是否相同,其中相同的命名空間別名會自動發生在您身上 - ?