c# - 在 Xdocument 中添加 xmlns 命名空间

标签 c# .net linq-to-xml

我想创建一个如下所示的 XDocument:

<configurations xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://msn.com/csl/featureConfigurationv2">
  <configuration>
    …
  </configuration>
</configurations>

我在添加第二个属性时遇到问题。我正在尝试这个:

XYZ.Element("configurations").SetAttributeValue("xmlns", "http://msn.com/csl/featureConfigurationv2");

但它没有添加属性。

你能推荐点别的吗

最佳答案

试试这个

XNamespace ns = XNamespace.Get("http://msn.com/csl/featureConfigurationv2"); 
XDocument doc = new XDocument(
    // Do XDeclaration Stuff
    new XElement("configurations",
        new XAttribute(XNamespace.Xmlns, ns),
        // Do XElement Stuff
     )
);

还有这种方式

XNamespace ns = "http://msn.com/csl/featureConfigurationv2";
XElement configurations = new XElement(ns + "configurations",
    new XAttribute("xmlns", "http://msn.com/csl/featureConfigurationv2"),
    // Do XElement Stuff
);

关于c# - 在 Xdocument 中添加 xmlns 命名空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8575428/

相关文章:

c# - 创建同步方法的异步版本

c# - 查找与分组

C#序列化十进制到xml

c# - 对循环缓冲区进行排序的最有效方法

c# - 缓存信息存储在 ASP.NET 中的什么位置?

c# - 有什么工具可以在.NET 程序执行时查看变量的存储位置?是在栈上还是堆上?

c# - .NET 反射 : How to call method of interface without creating instance?

c# - XML LINQ 查询不返回任何数据

c# - Linq to XML 通过查询子元素获取父元素属性值

c# - 如何合并两个 XDocuments 以删除重复项