tags:

views:

31

answers:

1

I'd like to write out the following XAML using LINQ to XML via C#.

<Activity x:Class="WorkflowConsoleApplication1.Activity1" mva:VisualBasic.Settings="Assembly references and imported namespaces for internal implementation"> </Activity> 

How do I specify the XNamespace for doing this to achieve the above output?

+1  A: 

This code will do it:

var el = new XElement(
    "Activity",
    new XAttribute(XName.Get("Class", "SomeNamespace"), "WorkflowConsoleApplication1.Activity1"),
    new XAttribute(
        XName.Get("VisualBasic.Settings", "SomeOtherNamespace"),
        "Assembly references and imported namespaces for internal implementation"));
Console.WriteLine(el.ToString());

Notice how you do not specify the prefixes, but rather the namespaces that these attributes belong to. This is by design and consistent with the XML spec. Prefixes will be generated automatically, or picked up from the containing element if this element is a child of another element that has already defined a prefix for the namespaces you're using.

Andrew Arnott
That's pretty much how I have it but it does not cut it.If I load the generated XAML in the Work Flow 4.0 designer I get the following error:Could not find member '{x}Class'.Could not find member '{mva}VisualBasic.Settings'.if I then remove the aliases that LINQ2XML generates and replace them with 'x' and 'mva' then that does the trick so what gives?
Abhijeet Patel
Wow. If that's the case that's a pretty broken-sounding XAML reader. Surprising. Are you *sure* you're leaving the xmlns declarations in the XML that my code sample emits?
Andrew Arnott
Otherwise I know you can use the old XML object model (XmlDocument) to control the prefixes that are used by initializing an `XmlNamespaceManager`
Andrew Arnott
I guess the XAML reader is perhaps broken because I can't get this to work using LINQ2XML. Marking your answer as the accepted answer
Abhijeet Patel