C#-将XML命名空间(xmlns)标记添加到文档 [英] C# - Add XML Namespace (xmlns) tag to document

查看:630
本文介绍了C#-将XML命名空间(xmlns)标记添加到文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C#中的System.XML创建XML文档.

I'm creating an XML document using System.XML in C#.

我快完成了,但是我需要在文档顶部添加类似于以下内容的内容:

I'm almost done, but I need to add some similar to the following to the top of my document:

<ABC xmlns="http://www.acme.com/ABC" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" fileName="acmeth.xml" date="2011-09-16T10:43:54.91+01:00" origin="TEST" ref="XX_88888">

我需要在我的位置下方添加它

I need to add this just below where I have:

<?xml version="1.0" encoding="UTF-8"?>

我使用以下代码创建它:

I create this using the following code:

XmlWriterSettings settings = new XmlWriterSettings { Encoding = Encoding.UTF8, Indent = true };

此后,我继续创建我的XML文档,该文档现在已完成,但是需要在两者之间添加.

After this I go on to create my XML document, which is finished now but I need to add this in-between.

谢谢

约翰

推荐答案

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

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); 
    }
}

这篇关于C#-将XML命名空间(xmlns)标记添加到文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆