Using relative URI as XmlDataProvider's Source

点点圈 提交于 2019-12-13 18:23:51

问题


Is it possible to use a relative URI when setting the Source property on an XmlDataProvider object in .NET? I get the following exception:

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

When I set the Source property using an absolute URI, everything works as expected:

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

However when I try using a relative URI I get the exception:

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

My assemblies are all located in the same directory as the configuration file. What's wrong here?


回答1:


Try this: FileInfo file = new FileInfo("configuration.xml"); provider.Source = new System.Uri(file.FullName);




回答2:


Yes, the following solves both document loading and using relative source path issues. Using a XmlDataProvider, which is defined in xaml, leaving the Source empty (should be possible in code too) :

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

The data provider automatically loads the document once the source is set. Here's the code :

    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);


来源:https://stackoverflow.com/questions/1059916/using-relative-uri-as-xmldataproviders-source

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!