0

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. I am doing using xmldataprovider using its source property

<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"

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

andrew
  • 19
  • 1
  • 8

1 Answers1

1

There is no such possibility using the Source property. The Source represents an Uri based on which a WebRequest is created fetching the data using a 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.

Something like:

XmlDocument doc = new XmlDocument();
using (FileStream s = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
    doc.Load(s);
}

LoadData.Document = doc;
dymanoid
  • 14,771
  • 4
  • 36
  • 64
  • Thank You very much dymanoid, ...meantime I just thought I will catch the exception and continue my infinite loop ...I was wrting to xml file every 5 sec in another program...and I was getting exception there..this worked for me for now...(not a good way though) However, I will try your suggestion in sometime now. Thanks again:) – andrew Oct 20 '17 at 17:40