且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

XML:设置命名空间元素的值

更新时间:2023-11-24 16:51:52

一个 元素?

如链接问题的第一个答案所述:dc 不是元素名称.这是一个命名空间名称.

As noted in the first answer to the linked question: dc is not an element name. It is a namespace name.

要添加命名空间中的元素,请使用适用的方法(可能是重载)来指定命名空间.但是,这取决于您使用的解析器(您没有指定).

To add an element that is in a namespace use the applicable method (maybe an overload) to specify the namespace. However this depends on the parser you are using (and you don't specify).

例如.使用 .NET 的 LINQ to XML 创建一个 XNamespace 来自命名空间的 URI,然后,当它重载 + 运算符时,您将其添加到元素名称之前:

Eg. with .NET's LINQ to XML you create an instance of XNamespace from the namespace's URI and then, as it overloads the + operator, you prepend it to the element name:

var ns = new XNamespace(namespaceUri);
var newElement = doc.firstNode.Add(new XElement(ns + "ElementName"));