简体   繁体   English

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

[英]How can I use ':' character in a name of XDocument element?

I'm using XDocument to create a RSS same below code:我正在使用 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"),

An exception occurring during the execution of this code.在执行此代码期间发生异常。

The ':' character, hexadecimal value 0x3A, cannot be included in a name. ':' 字符,十六进制值 0x3A,不能包含在名称中。

How can I use : character in a name of element?如何在元素名称中使用:字符?

To use namespaces, you'll need to create the namespace object first:要使用命名空间,您需要先创建命名空间 object:

UPDATED更新

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"),
            ....

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

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