简体   繁体   中英

Writing out namespace attributes in LINQ to XML

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM