locked
Assign the Source property of XmlDataProvider dynamically through code? RRS feed

  • Question

  • How can you assign the Source property of XmlDataProvider dynamically through code?
    Wednesday, March 10, 2010 5:11 AM

Answers

  • You could also try setting the XmlDataProvider.Document property to the XmlDocument.

    Margaret
    • Marked as answer by MyKuLLSKI Wednesday, March 10, 2010 10:19 PM
    Wednesday, March 10, 2010 9:53 PM
  • Wow Genius! Thank you so much Margaret Parsons

    Code:

            private void RebuildTree()
            {
                XmlDataProvider provider = new XmlDataProvider();
                XmlDocument xmlFile = new XmlDocument();
                xmlFile.Load(@"..\..\TreeData.xml");
                provider.Document = xmlFile;
                provider.XPath = "/AllBehaviors";

                treeview.DataContext = provider;
            }
    • Marked as answer by MyKuLLSKI Wednesday, March 10, 2010 10:19 PM
    Wednesday, March 10, 2010 10:19 PM

All replies

  • I believe we just need to assign an XmlDataProvider object to a DataContext. Here is an example.

    Xml (family.xml):
    <?xml version="1.0" encoding="utf-8" ?>
    <Family>
      <Person Name="Tom" Age="11"/>
      <Person Name="John" Age="12"/>
      <Person Name="Melissa" Age="38"/>
    </Family>

    Markup:
    <Window x:Class="XmlDataProviderInCode.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Height="300" Width="300">
        <StackPanel>
            <ListBox Name="listbox" ItemsSource="{Binding}">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding XPath=@Name}"/>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </StackPanel>
    </Window>

    Code:
    using System;
    using System.Windows;
    using System.Windows.Data;

    namespace XmlDataProviderInCode
    {
        public partial class Window1 : Window
        {
            public Window1()
            {
                InitializeComponent();
                listbox.DataContext = new XmlDataProvider { Source = new Uri("family.xml", UriKind.RelativeOrAbsolute), XPath = "/Family/Person" };
            }
        }
    }

    The full project example is at http://cid-7f542afbbb49dc0c.skydrive.live.com/self.aspx/.Public/Programming/XmlDataProviderInCode.zip


    Hope it helps.


    William
    Wednesday, March 10, 2010 6:37 AM
  • i dont think that worked how i wanted it to but maybe you can help me.
    what i am trying to do is check an xml file if it has been modified and if it has been modified to rebuild the tree that uses the xml file 
    below is what i have tried any suggestion?

            public MainWindow()
            {
                InitializeComponent();

                //This is for dynamically building a treeview with templates from an XML file
                xmlReader1 = new XmlTextReader("HierarchicalDataTemplate1.xml");
                hierarchicalDataTemplate1 = XamlReader.Load(xmlReader1) as HierarchicalDataTemplate;

                xmlReader2 = new XmlTextReader("HierarchicalDataTemplate2.xml");
                hierarchicalDataTemplate2 = XamlReader.Load(xmlReader2) as HierarchicalDataTemplate;
                hierarchicalDataTemplate1.ItemTemplate = hierarchicalDataTemplate2;

                xmlReader3 = new XmlTextReader("HierarchicalDataTemplate3.xml");
                hierarchicalDataTemplate3 = XamlReader.Load(xmlReader3) as HierarchicalDataTemplate;
                hierarchicalDataTemplate2.ItemTemplate = hierarchicalDataTemplate3;

                this.treeview.ItemTemplate = hierarchicalDataTemplate1;

                CheckXML();
            }

    public void CheckXML()
            {

                // Create a new FileSystemWatcher and set its properties.
                FileSystemWatcher watcher = new FileSystemWatcher();
                watcher.Path = @"..\..\";

                // Watch for changes in LastAccess and LastWrite times, and the renaming of files or directories.
                //watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;
                watcher.NotifyFilter = NotifyFilters.LastWrite;

                // Only watch TreeData.xml file.
                watcher.Filter = "TreeData.xml";

                // Add event handlers.
                watcher.Changed += new FileSystemEventHandler(XMLChanged);

                // Begin watching.
                watcher.EnableRaisingEvents = true;

            }

            private void XMLChanged(object source, FileSystemEventArgs e)
            {
                Thread updateThread = new Thread(new ParameterizedThreadStart(UpdateTree));
                updateThread.Start(this);
            }

            private void UpdateTree(object obj)
            {
                MainWindow wnd = (MainWindow)obj;
                wnd.Dispatcher.Invoke(DispatcherPriority.Send, new Action(RebuildTree));
            }

            private void RebuildTree()
            {
                //Deletes the entire tree
                this.treeview.ItemTemplate = null;
            
                //This is for dynamically building a treeview with templates from an XML file
                xmlReader1 = new XmlTextReader("HierarchicalDataTemplate1.xml");
                hierarchicalDataTemplate1 = XamlReader.Load(xmlReader1) as HierarchicalDataTemplate;

                xmlReader2 = new XmlTextReader("HierarchicalDataTemplate2.xml");
                hierarchicalDataTemplate2 = XamlReader.Load(xmlReader2) as HierarchicalDataTemplate;
                hierarchicalDataTemplate1.ItemTemplate = hierarchicalDataTemplate2;

                xmlReader3 = new XmlTextReader("HierarchicalDataTemplate3.xml");
                hierarchicalDataTemplate3 = XamlReader.Load(xmlReader3) as HierarchicalDataTemplate;
                hierarchicalDataTemplate2.ItemTemplate = hierarchicalDataTemplate3;

                this.treeview.ItemTemplate = hierarchicalDataTemplate1;
            }
    Wednesday, March 10, 2010 4:03 PM
  • You could also try setting the XmlDataProvider.Document property to the XmlDocument.

    Margaret
    • Marked as answer by MyKuLLSKI Wednesday, March 10, 2010 10:19 PM
    Wednesday, March 10, 2010 9:53 PM
  • Wow Genius! Thank you so much Margaret Parsons

    Code:

            private void RebuildTree()
            {
                XmlDataProvider provider = new XmlDataProvider();
                XmlDocument xmlFile = new XmlDocument();
                xmlFile.Load(@"..\..\TreeData.xml");
                provider.Document = xmlFile;
                provider.XPath = "/AllBehaviors";

                treeview.DataContext = provider;
            }
    • Marked as answer by MyKuLLSKI Wednesday, March 10, 2010 10:19 PM
    Wednesday, March 10, 2010 10:19 PM