且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C#-将XML命名空间(xmlns)标记添加到文档

更新时间:2022-03-03 02:23:22

我想这就是您要追求的目标:

I think this is what you're after:

using System;
using System.Xml.Linq;

class Test
{
    static void Main()
    {
        XNamespace ns = "http://www.acme.com/ABC";
        DateTimeOffset date = new DateTimeOffset(2011, 9, 16, 10, 43, 54, 91,
                                                 TimeSpan.FromHours(1));
        XDocument doc = new XDocument(
            new XElement(ns + "ABC",
                         new XAttribute("xmlns", ns.NamespaceName),
                         new XAttribute(XNamespace.Xmlns + "xsi",
                              "http://www.w3.org/2001/XMLSchema-instance"),
                         new XAttribute("fileName", "acmeth.xml"),
                         new XAttribute("date", date),
                         new XAttribute("origin", "TEST"),
                         new XAttribute("ref", "XX_88888")));

        Console.WriteLine(doc); 
    }
}