开发者

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

开发者 https://www.devze.com 2023-03-08 12:35 出处:网络
I\'m using XDocument to create a RSS same below code: var document = new XDocument( new XDeclaration(\"1.0\", \"utf-8\", null),

I'm using XDocument to create a RSS same below code:

var document = new XDocument(
    new XDeclaration("1.0", "utf-8", null),
    new XElement("rss",
                 new XElement("channel",
                              new XElement("title", "test"),
                              new XElement开发者_如何学C("dc:creator", "test"),

An exception occurring during the execution of this code.

The ':' character, hexadecimal value 0x3A, cannot be included in a name.

How can I use : character in a name of element?


To use namespaces, you'll need to create the namespace object first:

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号