The forums have permanently moved to This forum will be kept around in read-only mode for archival purposes. To learn how to continue using your existing account on the new forums, check out this thread.

Results 1 to 5 of 5

Thread: Change XmlDataProvider with OpenFileDialog

  1. #1

    Change XmlDataProvider with OpenFileDialog

    Hello. I am trying to make a small WPF app that will let me view images and text contained in an xml document. I have all the binding set up. Now I am trying to find a way to load a new xml file (same schema). I am trying to use the OpenFileDialog to specify the source property of the XmlDataProvider shown in my XAML to do this. The app compiles no problem, but nothing changes when I "open" a new xml file. Any help would be great!

    //Here is the C# code behind

    using System;
    using System.IO;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Net;
    using System.Xml;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Shapes;
    using Microsoft.Win32;
    namespace WpfApplication2
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    public Window1()
    // Insert code required on object creation below this point.
    //Saves new XML data added to text boxes
    private void write_XML_record(object sender, RoutedEventArgs e)
    //Close the app
    private void Close_XML_Writer(object sender, RoutedEventArgs e)
    //Uses OpenFileDialog to specify the source property of XmlDataProvider
    private void Import_Metadata_XML(object sender, RoutedEventArgs e)
    OpenFileDialog openFileDialog1 = new OpenFileDialog();
    openFileDialog1.Filter = "XML Files (Metadata.xml)|*.xml";
    openFileDialog1.FilterIndex = 1;
    openFileDialog1.Multiselect = false;
    bool? userClickedOK = openFileDialog1.ShowDialog();
    if (userClickedOK == true)
    string filename = openFileDialog1.FileName;
    XmlDataProvider MetadataDS = new XmlDataProvider();
    MetadataDS.Source = new Uri(filename);
    //hook up to scroll bar to cycle through XML records
    private void JumpToPreviousXML(object sender, MouseButtonEventArgs e)
    //hook up to scroll bar to cycle through XML records
    private void JumpToNextXML(object sender, MouseButtonEventArgs e)
    //Here is the XAML related to the XmlDataProvider

    Title="XML Writer"
    Width="640" Height="400" Icon="PI Icon.png" Background="#FFCECECE" xmlns:d="" xmlns:mc="" mc:Ignorable="d" xmlns:Microsoft_Windows_Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Luna">
    <XmlDataProvider x:Key="MetadataDS" Source="C:\Documents and Settings\Psychman\My Documents\Expression\Deep Zoom Composer Projects\CannonballImages_Test\source images\OutputSdi\collectiontest2\DeepZoomProjectWeb\ClientBin\GeneratedImages\Metadata.xml" d:IsDataSource="True"/>

  2. #2
    Hi psychman!
    I don't have a machine with VS / Blend installed right now, but the reason it isn't working is because you need to actually get a reference to your existing XmlDataProvider and then update it from there. This is almost identical to the Modifying a Databound Collection case:

    Looking at your code, you would need to something similar to the following.
    XmlDataProvider existing = Window.Resources["MetadataDS"] as XmlDataProvider;
    existing.Source = new Uri(filename);
    I am writing the above without the use of VS, so there is a chance it might not work. Let me know if it fails and I can look into it further


    Great, now even Kirupa is { facebooking | twittering }

  3. #3
    Hi Kirupa. Hey, thanks for the help. Worked like a charm!

  4. #4
    Quote Originally Posted by kirupa View Post
    This is almost identical to the Modifying a Databound Collection case:
    Thanks again Kirupa. I went back over the above post and it helped me answere some of the questions I had, but I do have one more if you don't mind. I have this little project almost wrapped up now. When complete the app will do the following:

    -Imports Metadata.xml file generated by Deep Zoom Composer
    -On import it will populate the databound image and text boxes with the original data
    -On import it will also add four new "tag" elements to the original xml file
    -The user will be able to navigate all of the original images and coresponding tags (your MIX08 presentation helped me create the navigation piece ((thanks!))
    -The user can add new "tag" data to any of the old and new fields and the new data will overwrite the original xml file when the "save" button is clicked.

    I have everything working great. Now I just need to figure out how to take my new "tag" elements and add them into all of the nodes on import. I know how to do this to one node, but I am not sure how to add the new elements to every "Image" node in the file. Here is the code I am using to make this work for the first node:

    XmlDocument doc = new XmlDocument();
    doc.Load (variable pointing to xml file);
    XmlDocumentFragment docFrag = doc.CreateDocumentFragment();
    docFrag.InnerXml = "<testItem> test </testItem>";

  5. #5
    Scratch that last post. I found what I needed. I removed the line of code:

    doc.DocumentElement.FirstChild.NextSibling.AppendC hild(docFrag);

    and replaced it with:

    foreach (XmlNode node in doc.DocumentElement.ChildNodes)

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Home About Meet the Moderators Advertise

 Link to Us


Copyright 1999 - 2012