如何使用';:';XDocument元素名称中的字符

本文关键字:字符 XDocument 何使用 元素 | 更新日期: 2023-09-27 17:57:49

我正在使用XDocument创建一个RSS,代码如下:

var document = new XDocument(
    new XDeclaration("1.0", "utf-8", null),
    new XElement("rss",
                 new XElement("channel",
                              new XElement("title", "test"),
                              new XElement("dc:creator", "test"),

在执行此代码期间发生的异常。

":"字符(十六进制值0x3A)不能包含在名称中。

如何在元素名称中使用:字符?

如何使用';:';XDocument元素名称中的字符

要使用名称空间,您需要首先创建名称空间对象:

更新

XNamespace ns = "http://purl.org/dc/elements/1.1/";
var document = new XDocument(
            new XDeclaration("1.0", "utf-8", null),
            new XElement("rss", new XAttribute(XNamespace.Xmlns + "dc", ns)
                         new XElement("channel",
                                      new XElement("title", "test"),
                                      new XElement(ns + "creator", "test"),
            ....