2009-06-29 29 views
0

在.NET中設置XmlDataProvider對象時,是否可以使用相對URI來設置Source屬性?我得到以下異常:使用相對URI作爲XmlDataProvider的源代碼

IOException:System.IO.IOException: Cannot locate resource 'configuration.xml'. 

當我設置使用絕對URI的Source財產,一切正常:

provider.Source = new Uri(@"C:\bin\Configuration.xml", UriKind.Absolute); 

然而,當我嘗試使用相對URI我得到異常:

provider.Source = new Uri(@"Configuration.xml", UriKind.Relative); 

我的程序集都位於與配置文件相同的目錄中。這裏有什麼問題?

回答

1

試試這個: FileInfo file = new FileInfo(「configuration.xml」); provider.Source = new System.Uri(file.FullName);

0

是的,以下解決了文檔加載和使用相對源路徑問題。使用XmlDataProvider,這是在XAML定義,留下源空(應該是在代碼可能太):

<Window.Resources> 
<XmlDataProvider 
    x:Name="myDP" 
    x:Key="MyData" 
    Source="" 
    XPath="/RootElement/Element" 
    IsAsynchronous="False" 
    IsInitialLoadEnabled="True"       
    debug:PresentationTraceSources.TraceLevel="High" /> </Window.Resources> 

一旦源被設定的數據提供自動加載文檔。代碼如下:

m_DataProvider = this.FindResource("MyData") as XmlDataProvider; 
    FileInfo file = new FileInfo("MyXmlFile.xml"); 

    m_DataProvider.Document = new XmlDocument(); 
    m_DataProvider.Source = new Uri(file.FullName);