2009-06-29 16 views
0

.NETのXmlDataProviderオブジェクトのSourceプロパティを設定するときに相対URIを使用できますか?私は、次の例外を取得:私は絶対URIを使用してSourceプロパティを設定すると相対URIをXmlDataProviderのソースとして使用する

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

、すべてが期待通りに動作します。私は、相対URIを使用しようとすると、

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

は、しかし、私は例外を取得:

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

私のアセンブリは、すべて構成ファイルと同じディレクトリにあります。ここで何が間違っていますか?

答えて

1

これを試してください: FileInfo file = new FileInfo( "configuration.xml"); provider.Source = new System.Uri(file.FullName);

0

はい、以下は、ドキュメントの読み込みと相対的なソースパスの問題の両方を解決します。 (すぎるコードで可能でなければならない)空のソースを残して、XAMLで定義されているにXmlDataProviderを使用して:

<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); 
関連する問題