Archiv der Kategorie: XML

Slippery When Wet #7.1: Binding between XElement and ComboBox/ListBox Revisited

In Slippery When Wet #7: Binding between XElement and ComboBox/ListBox I wrote that the problem should be fixed with a hotfix from Microsoft. Meanwhile I’ve got the Hotfix installed on my System.

And the Hotfix solved the problem! So if you plan to use TwoWay binding between XElement objects and a ComboBox or ListBox get the Hotfix from Microsoft. According to the description the Hotfix should be included in a future software update. But in which update this fix will be included is not written there. So check the version numbers of the files listed in the description to see if you still need the Hotfix.

Note: According to the description installing the hotfix should not require a restart of the computer. But on the two Systems where we installed the Hotfix (one 32bit and one 64bit system) we had to restart both systems.

Slippery When Wet #7: Binding between XElement and ComboBox/ListBox

I proudly present to you the seventh in a infinite number of posts of “Slippery When Wet.” In these posts I show you a little bastard I stumbled on.

In Slippery When Wet #6: XPath and the Default Namespace I already ran in some problems with XML in .NET. And I’ve done it again!

For a dynamically built User Interface I’ve bound some controls to a XDocument tree that contained the data and the description of the types. The first controls (TextBox and a numeric Control) worked perfectly, so I was confident that the next controls would just be a routine work. So the next control I wanted to use was the ComboBox.

The data was shown as expected, but after changing the selection the new data was not saved. I’ve tried several changes in the binding (adding explicit two-way binding, setting the UpdateSourceTrigger to PropertyChanged) but the data was not updated when the selection changed. I’ve asked some guys in the office with more WPF experience, but they also saw no reason why the binding didn’t work. Even adding a Converter to check if the data types were not compatible gave me no clue. I’ve also asked on Stack Overflow but got no solution for my problem.

After asking Dr. Google the right question I’ve found these two entries in the MSDN Forum:
MSDN Forum: ComboBox that binds to XElement breaks in .NET 4 and
ComboBox and DataGridComboBoxColumn two-way data binding broken in .NET 4 with XLinq for the SelectedItem/SelectedItemBindings property
which guided me to the Hot Fix from Microsoft (The „TwoWay“ binding does not reflect changes when the property path of the binding refers to the Attribute or Element method of an XElement object in a .NET Framework 4.0-based WPF application).

So it looks like I’ve done it right this time but Microsoft didn’t. So now I’m waiting for getting the Hot Fix installed on my System to check if it works now.

Slippery When Wet #6: XPath and the Default Namespace

I proudly present to you the sixth in a infinite number of posts of “Slippery When Wet.” In these posts I show you a little bastard I stumbled on.

Recently, I tried to work on a XmlDocument with XPath in C#. To get the right expressions easily I used the XML Spy from Altova. As soon as I got the right XPath expressions I started Visual Studio to work in my .NET Application. But here, the only expression that worked was „/*“. According to the .NET Documentation the expressions should work, but they didn’t.

So I tried another XML file, one from the examples in the .NET Documentation of XPath. And with this file the (corresponding) expressions worked as expected. So I searched the difference of the two XML files. After a short time I found the relevant difference: The file that worked had no namespace defined, whereas the non-working file had two namespaces. One namespace with a prefix (xmlns:prefix=“URI“) and a default namespaces (xmlns=“URI“).

As I got the reason for my problem I had to look for a solution. A little bit of googling brought me the solution. I had to add the default namespace to the NamespaceManager with a prefix and then use the XPath expressions with the defined prefix.

In .NET 3.5 Microsoft introduced the XDocument class as a alternative (or a replacement) for XmlDocument. The C# code differs a little bit depending on the used class. I found a description when using the XmlDocument class and two when using the XDocument class.

With the added prefix for the default namespace my XPath expressions worked as expected.