Writing out namespace attributes in LINQ to XML
I'd like to write out the foll开发者_如何学运维owing 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.
精彩评论