Schlagwort-Archive: XML

Reflektion KW 41

Image courtesy of "marcolm" / FreeDigitalPhotos.net

Image courtesy of „marcolm“ / FreeDigitalPhotos.net

Ich habe mir zu Ziel gesetzt, jede Woche hier im Blog kurz zu reflektieren. Diese Woche starte ich und versuche, dies zumindest bis Ende Jahr durchzuziehen.

Pair Programming
Diese Woche habe ich an zwei Tagen mit zwei verschiedenen Team-Mitgliedern im Pair Programming an zwei Tasks gearbeitet. Es waren zwei intensive Tage, die aber zu guten Ergebnissen in kurzer Zeit geführt haben.

Halbwertszeit des Wissens
Obwohl ich selbst über das Problem mit den Namespaces in XML gebloggt hatte bin ich gut zwei Jahre später wieder in dieselbe Falle getappt. Aber wenigstens ist mir schnell eingefallen, dass ich dieses Problem schon einmal hatte.

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.