views:

1946

answers:

2

Every sample that I have seen uses static XML in the xmldataprovider source, which is then used to databind UI controls using XPath binding. Idea is to edit a dynamic XML (structure known to the developer during coding), using the WPF UI.

Has anyone found a way to load a dynamic xml string (for example load it from a file during runtime), then use that xml string as the XmlDataprovider source?

Code snippets would be great.

Update: To make it more clear, Let's say I want to load an xml string I received from a web service call. I know the structure of the xml. So I databind it to WPF UI controls on the WPF Window. How to make this work? All the samples over the web, define the whole XML inside the XAML code in the XmlDataProvider node. This is not what I am looking for. I want to use a xml string in the codebehind to be databound to the UI controls.

+1  A: 

using your webservice get your XML and create an XML Document from it, You can then set the Source of your xmlDataProvider to the XMLDocument you got from the service.

I'm not at a pc with visual studio to test it but it should be possible for you to do this.

The steps are as you mentioned in your question:

1. Get XML from webservice
2. Convert XML String to XML Document
3. Set the XMLDataProvider.Document value to your XML Document
4. Bind that to your controls
Mauro
+1  A: 

Here is some code I used to load a XML file from disk and bind it to a TreeView. I removed some of the normal tests for conciseness. The XML in the example is an OPML file.

XmlDataProvider provider = new XmlDataProvider();

if (provider != null)
{
  System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
  doc.Load(fileName);
  provider.Document = doc;
  provider.XPath = "/opml/body/outline";
  FeedListTreeView.DataContext = provider;
}
Paul Osterhout