[英]How to read an xml file using xmldataprovider in wpf C#
I have a xaml file with my wpf controls defined, I am binding all its label controls to an xml file and populating from this file. 我有一个定义了wpf控件的xaml文件,我将其所有标签控件绑定到xml文件并从该文件填充。 I am doing using xmldataprovider using its source property 我正在使用xmldataprovider使用其source属性
<Grid.DataContext>
<XmlDataProvider x:Name="LoadData" Source="data.xml" XPath="Loads/*" Document=/>
</Grid.DataContext>
<Label Grid.Row="1" Name="textbox1" Grid.Column="0" Grid.RowSpan="3" Grid.ColumnSpan="2" Background="Gray" BorderThickness="2" Content="{Binding XPath=teamname, Mode=OneWay}" FontSize="36">
and in the code behind, 在后面的代码中
string filename = "C:\\data.xml";
LoadData.Source = new Uri(filename);
Everything works fine, my only problem is i want to open this xml in read only mode as one of another program is writing to it and i get exception of" being used by another program" 一切正常,我唯一的问题是我想在只读模式下打开此xml,因为另一个程序正在向其中写入它,并且出现“正在被另一个程序使用”的异常
is there any such provision from xmldataprovider to set the source/read the xml file in data provider..Has anyone does this before...input/suggestions are welcome...many thanks xmldataprovider是否提供任何此类设置来设置数据提供程序中的源文件/读取xml文件。是否有人在...之前欢迎输入/建议...非常感谢
There is no such possibility using the Source
property. 使用Source
属性没有这种可能性。 The Source
represents an Uri
based on which a WebRequest
is created fetching the data using a Stream
. Source
表示一个Uri
基于该Uri
创建一个WebRequest
,以使用Stream
获取数据。 You cannot control how this stream will be created though. 但是,您无法控制如何创建此流。
There is a workaround; 有一个解决方法。 however, you have to do that in code. 但是,您必须在代码中执行此操作。 You can manually load your XML document and assign it to the Document
property of the XmlDataProvider
. 您可以手动加载XML文档,并将其分配给XmlDataProvider
的Document
属性。
Something like: 就像是:
XmlDocument doc = new XmlDocument();
using (FileStream s = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
doc.Load(s);
}
LoadData.Document = doc;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.