Parsing XAML woes

So, I’ve been sitting here for a couple of hours trying to figure out why the following XML query didn’t actually work when parsing my XAML for the following item:

<Image Height=”16″ Width=”16″ Source=”Images/MyImage.png” x:Key=”CommandNew” />



The XAML loaded into an XML document just fine. It’s well formed, and the Image item does exist inside the Application.Resources section. I just couldn’t figure out what the heck was going on. Then, it hit me – in a flash of the blinding, bleeding obvious. These tags don’t actually exist on their own. They are part of the XAML namespaces, which opens up a whole new line of things you need to do. Basically, in order to perform this query, I needed to import the same XAML namespaces into my XML document – and then I should be good to go. Right?

Well, not quite. The above query also needs to change slightly. Without, the namespaces in, this query still doesn’t work. So the first thing we need to do is add the namespaces into the query and we get:


and the code to load the namespaces in:

private static XmlNamespaceManager GetNamespace(XmlDocument doc)
XmlNamespaceManager mgr = new XmlNamespaceManager(doc.NameTable);
  return mgr;

Note how the namespaces need to prefix each item in the query. It doesn’t matter what we call them because they are mapped over to the relevant namespace as appropriate – the important thing is the Uri, not the name.

There, now we can query our XAML to our hearts content. I hope this has been of some benefit to you because it was really bugging me.;->



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s