C# – 将XML命名空间(xmlns)标记添加到文档
我正在使用C#中的System.XML创建XML文档。
我差不多完成了,但我需要在文档的顶部添加一些类似的内容:
我需要在下面的地方添加:
我使用以下代码创建它:
XmlWriterSettings settings = new XmlWriterSettings { Encoding = Encoding.UTF8, Indent = true };
在此之后,我继续创建我的XML文档,现在已经完成,但我需要在中间添加它。
谢谢
约翰
我想这就是你所追求的:
using System; using System.Xml.Linq; class Test { static void Main() { XNamespace ns = "https://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", "https://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); } }
您可以将命名空间声明添加到XmlDocument
的根元素,如下所示:
上述就是C#学习教程:C# – 将XML命名空间(xmlns)标记添加到文档分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
document.DocumentElement.SetAttribute("xmlns", "https://default-namespace"); document.DocumentElement.SetAttribute("xmlns:ns2", "https://other-namespace");
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/1013846.html