Monday, March 7, 2011

Load User Control dynamically and Default Namespace

In Silverlight, when you want to create a control dynamically, you must add the namespaces like this (as in http://msdn.microsoft.com/en-us/library/cc189044(VS.95).aspx):

XNamespace xmlns = "http://schemas.microsoft.com/client/2007";
    XElement textBlock2 = new XElement(xmlns + "TextBlock",
        new XAttribute(XNamespace.Xmlns + "x", "http://schemas.microsoft.com/winfx/2006/xaml"),
        ...
        );

My problem is that I have a user control in its own namespace, so I must write something like

XNamespace myxmlns = "mynamespace";
XElement myelem = new XElement(myxmlns + "MyCtrl", ...

I can then add aliased namespaces like that

new XAttribute(XNamespace.Xmlns + "x", "http://schemas.microsoft.com/winfx")

but I can't figure out how to add the default namespace. I either get a compile error or a run-time error ("AG E PARSER MISSING DEFAULT NAMESPACE"), whatever I try.

I succeeded doing it with building a big string of what I need, but I would like to understand what I am missing.

Any idea?

Thanks.

From stackoverflow
  • From the XNamespace docs here:

    new XAttribute("xmlns", "http://http://www.adventure-works.com")
    

    Just add it as an XAttribute.

    devMomentum : Thanks, was too easy... I love Linq to Xml, but the XNamespace is not that intuitive to me.

0 comments:

Post a Comment