开发者

How to create an Xml with multiple namescapes using XDocument

I would like to create a document with multiple namespaces (see Xml below). How can I do that using XDocument? My final result should the following:

<?xml version="1.0" encoding="utf-8"?>
<a:feed xmlns:a="http://www.w3.org/2005/Atom" xmlns:os="http://a9.com/-/spec/opensearch/1.1/" xmlns="http://schemas.zune.net/catalog/apps/2008/02">
  <a:link rel="prev" type="application/atom+xml" href="myUrl" />
  <a:link rel="nex开发者_StackOverflow社区t" type="application/atom+xml" href="myUrl" />
  <a:link rel="self" type="application/atom+xml" href="myUrl" />
  <a:updated>2008-05-20T22:50:46.7932864Z</a:updated>

Here is the code I have so far.

    XNamespace nsW3Atom = "http://www.w3.org/2005/Atom";
    XNamespace nsOs = "http://a9.com/-/spec/opensearch/1.1/";
    XNamespace nsZune = "http://schemas.zune.net/calatog/apps/2008/02";

    XDocument doc =
        new XDocument(
            // new XDeclaration("1.0", "utf-8", "no"),
            new XElement("feed",
                new XAttribute("a", nsW3Atom),
                new XAttribute("os", nsOs),
                new XAttribute("os2", nsZune),
                new XElement(XNamespace.Xmlns + "link", new XAttribute("rel", "prev"), new XAttribute("type", "application/atom+xml")),
                new XElement(XNamespace.Xmlns + "link", new XAttribute("rel", "next"), new XAttribute("type", "application/atom+xml")),
                new XElement(XNamespace.Xmlns + "link", new XAttribute("rel", "self"), new XAttribute("type", "application/atom+xml"))));

Thank you in advance for the help!


 XNamespace a="http://www.w3.org/2005/Atom";
 XNamespace os = "http://a9.com/-/spec/opensearch/1.1/";
 XNamespace def = "http://schemas.zune.net/catalog/apps/2008/02";

 var doc =
     new XDocument(new XElement(a + "feed", 
                         new XAttribute(XNamespace.Xmlns + "a", a),
                         new XAttribute(XNamespace.Xmlns + "os", os),
                         new XAttribute("xmlns", def)));

then if You want to use the a Namespace, prefix any element with a+


When creating the attributes use the xmlns: prefix.

            new XAttribute("xmlns:a", nsW3Atom),
            new XAttribute("xmlns:os", nsOs),
            new XAttribute("xmlns:os2", nsZune)
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜