我想创建一个如下所示的 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/