Everybody! This is important. In a few days, these forums will be moving over to using the totally sweet Discourse platform. To ensure this migration happens smoothly with no loss of content, these forums are currently in a read-only mode. I do apologize for the inconvenience.

There is never a good time to turn the forums off for an extended period of time, but I promise the new forums will be a billion times better. I'm pretty sure of it.

See you all on the other side in a few days, and if you have any (non-technical) questions, please e-mail me at kirupa@kirupa.com. For technical questions, try to find a tutorial that corresponds to what you are looking for and post in the comments section of that page.

Cheers,
Kirupa

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

    Code:
     
    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()
    {
    this.InitializeComponent();
    // 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)
    {
    Window.Close();
    }
    //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

    Code:
     
    <Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="WpfApplication2.Window1"
    x:Name="Window"
    Title="XML Writer"
    Width="640" Height="400" Icon="PI Icon.png" Background="#FFCECECE" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" xmlns:Microsoft_Windows_Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Luna">
    <Window.Resources>
    <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: http://www.kirupa.com/blend_wpf/modi...ection_pg1.htm

    Looking at your code, you would need to something similar to the following.
    Code:
    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

    Cheers!
    Kirupa

    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: http://www.kirupa.com/blend_wpf/modi...ection_pg1.htm
    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:

    Code:
     
    XmlDocument doc = new XmlDocument();
    doc.Load (variable pointing to xml file);
     
    XmlDocumentFragment docFrag = doc.CreateDocumentFragment();
    docFrag.InnerXml = "<testItem> test </testItem>";
    doc.DocumentElement.FirstChild.NextSibling.AppendChild(docFrag);
     
    doc.Save("Metadata.xml");

  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)
    {
    node.AppendChild(docFrag.Clone());
    }

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 kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012