简体   繁体   English

在LINQ to XML中写出名称空间属性

[英]Writing out namespace attributes in LINQ to XML

I'd like to write out the following XAML using LINQ to XML via C#. 我想通过C#使用LINQ to XML编写以下XAML。

<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? 如何指定XNamespace来实现上述输出?

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. 这是设计使然,并且与XML规范一致。 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. 前缀将自动生成,或者如果包含的另一个元素的子元素已经为您使用的名称空间定义了前缀,则从包含的元素中提取前缀。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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