Click here to Skip to main content
Click here to Skip to main content

WPF: XmlDataProvider Two-Way Data Binding - Enhanced

By , 11 Jan 2011
Rate this:
Please Sign up or sign in to vote.

Introduction

In my previous article, WPF: XmlDataProvider Two-Way Data Binding, I discussed how the XmlDataProvider does not natively support two-way binding and provided a simple workaround to address the issue. The binding in the previous article only demonstrated read and update operations. In response to several reader comments inquiring about insert and delete operations, I have updated the demo code and provided this brief article.

Background

The application in my previous article was developed with Visual Studio 2008 and targeted the 3.5 version of the .NET framework. The downloadable project above for this article was upgraded to Visual Studio 2010.

Using the Code

You will notice from the screenshot below that I have added two new buttons to the form for the purpose of inserting and deleting records. Each button has a corresponding event handler to persist the changes to the source XML file.

Screenshot.gif

We'll start with the New button and its event handler. The button simply removes the listbox selection and clears the Team form to prepare for a new record.

private void NewTeamButton_Click(object sender, RoutedEventArgs e)
{
     TeamsListBox.SelectedIndex = -1;
     TeamIDTextBox.Text = String.Empty;
     TeamNameTextBox.Text = String.Empty;
     ConferenceTextBox.Text = String.Empty;
}

The event handler for the Save button has been enhanced to accommodate for new records. In the previous article, the Save button simply persisted the XmlDataProvider's XmlDocument to the underlying source XML file. That approach works well for update operations as any changes to the UI are automatically synchronized in the XmlDataProvider's XmlDocument through its binding. The enhanced event handler will search the XmlDataProvider's XmlDocument, using XPath, for the Team ID located in the TeamIDTextBox. If the ID is not found, the code builds the necessary XML nodes to represent the new team and then adds it to the XmlDataProvider's XmlDocument. Lastly, the XmlDataProvider's XmlDocument is persisted to the source XML file.

private void SaveTeamsButton_Click(object sender, RoutedEventArgs e)
{
     string source = TeamData.Source.LocalPath;

     // Get a handle on the source data file.
     XmlDocument doc = TeamData.Document;

     // Get a handle on the root node.
     XmlNode root = doc.SelectSingleNode("//Teams");

     // Attempt to find the Id in the data file.
     string xpath = "//Team[Id=" + TeamIDTextBox.Text + "]";
     XmlNode currentNode = root.SelectSingleNode(xpath);

     // If currentNode is null, then we add a new team with the data entered.  If found,
     // we simply call the save method to persist the updated data to the data file.
     if (currentNode == null)
     {
          XmlNode newTeam = doc.CreateElement("Team");
          XmlNode newTeamId = doc.CreateElement("Id");
          XmlNode newTeamName = doc.CreateElement("Name");
          XmlNode newTeamConference = doc.CreateElement("Conference");

          newTeamId.InnerText = TeamIDTextBox.Text;
          newTeamName.InnerText = TeamNameTextBox.Text;
          newTeamConference.InnerText = ConferenceTextBox.Text;

          newTeam.AppendChild(newTeamId);
          newTeam.AppendChild(newTeamName);
          newTeam.AppendChild(newTeamConference);

          root.AppendChild(newTeam);
     }

     // Save the changes.
     TeamData.Document.Save(source);
}

You will notice that the Delete button event handler looks familiar. It uses the same code as the Save button to find the currently selected record within the XmlDataProvider's XmlDocument. If found, the XmlNode that represents the current record is removed from the XmlDocument and the changes are persisted to the source XML file.

private void DeleteTeamsButton_Click(object sender, RoutedEventArgs e)
{
     // Get the physical path for saving later.
     string source = TeamData.Source.LocalPath;

     // Get a handle on the source data file.
     XmlDocument doc = TeamData.Document;

     // Get a handle on the root node.
     XmlNode root = doc.SelectSingleNode("//Teams");

     // Get the currently selected node within the source data file.
     string xpath = "//Team[Id=" + TeamIDTextBox.Text + "]";
     XmlNode currentNode = root.SelectSingleNode(xpath);

     if (currentNode != null)
     {
          // Delete the record.
          root.RemoveChild(currentNode);

          // Save the changes.
          TeamData.Document.Save(source);
     }
}

Conclusion

Even though the XmlDataProvider has its limitations, they can easily be overcome with a small amount of code. I hope that this article has been informative.

History

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Ryan Bost
Software Developer (Senior)
United States United States
Ryan is a good ol' boy from North Carolina who is obsessed with .NET and general web design.
 
When he is not coding, he enjoys...
 
  • spending time with his wife -- knowledge base of locations for all my worldly possessions--honey, where's my...
  • spending time with his twin sons -- my mom says this is payback for my behavior as a child : )
  • playing with his dogs -- they can't hear me calling them to come inside, but they can hear a refrigerator open from a different hemisphere
  • watching college basketball -- Go Tarheels!
  • watching movies from his DVD collection -- over 750 and counting
  •  

Comments and Discussions

 
GeneralMy vote of 5 Pinmembertauqir_21-Jan-13 7:33 
GeneralMy vote of 4 Pinmemberyoave2320-Mar-12 0:47 
GeneralWorks out great, I think you are missing an update (it only inserts) here's extending the code Pinmemberhksingh11-May-11 11:46 
GeneralRe: Works out great, I think you are missing an update (it only inserts) here's extending the code PinmemberRyan Bost28-May-11 3:40 
GeneralMy vote of 2 PinmemberAlecTaylor12-Apr-11 5:01 
GeneralRe: My vote of 2 PinmemberRyan Bost28-May-11 3:52 
GeneralMy vote of 3 PinmemberSteve Hansen11-Jan-11 3:35 
GeneralRe: My vote of 3 PinmemberRyan Bost11-Jan-11 4:07 
GeneralMy vote of 1 PinmemberSteve Hansen11-Jan-11 2:43 
GeneralRe: My vote of 1 PinmemberRyan Bost11-Jan-11 3:27 
GeneralRe: My vote of 1 PinmemberSteve Hansen11-Jan-11 3:34 
GeneralRe: My vote of 1 PinmemberRyan Bost11-Jan-11 4:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140415.2 | Last Updated 11 Jan 2011
Article Copyright 2011 by Ryan Bost
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid