C# 如何使用XDocument创建具有多个命名空间的Xml

C# 如何使用XDocument创建具有多个命名空间的Xml,c#,.net,xml,linq-to-xml,C#,.net,Xml,Linq To Xml,我想创建一个具有多个名称空间的文档(请参见下面的Xml)。如何使用XDocument做到这一点?我的最终结果如下: <?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/0

我想创建一个具有多个名称空间的文档(请参见下面的Xml)。如何使用XDocument做到这一点?我的最终结果如下:

<?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="next" 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>

提前感谢您的帮助

创建属性时,请使用
xmlns:
前缀

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

然后,如果要使用名称空间,请在任何元素前面加上+

谢谢Richard。那么xmlns=“”呢?如何在元素前面加上“a:”?默认名称空间定义为:new XAttribute(“xmlns”),为元素使用XNamespace,请参阅
            new XAttribute("xmlns:a", nsW3Atom),
            new XAttribute("xmlns:os", nsOs),
            new XAttribute("xmlns:os2", nsZune)
 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)));