如何强制 XmlWriter 正确处理命名空间?

发布于 2024-11-28 10:43:08 字数 1770 浏览 1 评论 0原文

我想使用 XmlWriter 编写类似这样的内容(全部在一个命名空间中):

<Root xmlns="http://tempuri.org/nsA">
  <Child attr="val" />
</Root>

但我似乎能得到的最接近的是:

<p:Root xmlns:p="http://tempuri.org/nsA">
  <p:Child p:attr="val" />
</p:Root>

使用此代码:

using System;
using System.Text;
using System.Xml;

namespace ConsoleApplication1
{
    internal class Program
    {
        private const string ns = "http://tempuri.org/nsA";
        private const string pre = "p";

        private static void Main(string[] args)
        {
            var sb = new StringBuilder();
            var settings = new XmlWriterSettings
                     {
                       NamespaceHandling = NamespaceHandling.OmitDuplicates, 
                                             /* ineffective */             
                       Indent = true
                     };
            using (XmlWriter writer = XmlWriter.Create(sb, settings))
            {
                writer.WriteStartElement(pre, "Root", ns);
                writer.WriteStartElement(pre, "Child", ns);
                writer.WriteAttributeString(pre, "attr", ns, "val"); 
                                             // breaks namespaces    
                writer.WriteEndElement();
                writer.WriteEndElement();
            }
            Console.WriteLine(sb.ToString());
        }
    }
}

当我不指定前缀时,我得到:

<Root xmlns="http://tempuri.org/nsA">
  <Child p2:attr="val" xmlns:p2="http://tempuri.org/nsA" />
</Root>

这些“幻像”的生成" 重复命名空间中的前缀出现在整个生成的文档中(p3、p4、p5 等)。

当我不写属性时,我会得到我想要的输出(显然它缺少属性)。

为什么 XmlWriter 不像我问的那样省略重复的命名空间?

I want to use XmlWriter to write something like this (all in one namespace):

<Root xmlns="http://tempuri.org/nsA">
  <Child attr="val" />
</Root>

but the closest I can seem to get is this:

<p:Root xmlns:p="http://tempuri.org/nsA">
  <p:Child p:attr="val" />
</p:Root>

using this code:

using System;
using System.Text;
using System.Xml;

namespace ConsoleApplication1
{
    internal class Program
    {
        private const string ns = "http://tempuri.org/nsA";
        private const string pre = "p";

        private static void Main(string[] args)
        {
            var sb = new StringBuilder();
            var settings = new XmlWriterSettings
                     {
                       NamespaceHandling = NamespaceHandling.OmitDuplicates, 
                                             /* ineffective */             
                       Indent = true
                     };
            using (XmlWriter writer = XmlWriter.Create(sb, settings))
            {
                writer.WriteStartElement(pre, "Root", ns);
                writer.WriteStartElement(pre, "Child", ns);
                writer.WriteAttributeString(pre, "attr", ns, "val"); 
                                             // breaks namespaces    
                writer.WriteEndElement();
                writer.WriteEndElement();
            }
            Console.WriteLine(sb.ToString());
        }
    }
}

When I don't specify a prefix, I get:

<Root xmlns="http://tempuri.org/nsA">
  <Child p2:attr="val" xmlns:p2="http://tempuri.org/nsA" />
</Root>

The generation of these "phantom" prefixes in duplicate namespaces occurs throughout the generated document (p3, p4, p5 etc).

When I don't write attributes, I get the output I want (except it's missing the attributes, obviously).

Why isn't XmlWriter omitting duplicate namespaces like I asked?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笑咖 2024-12-05 10:43:08

尝试这样:

using System;
using System.Text;
using System.Xml;

class Program
{
    private const string ns = "http://tempuri.org/nsA";

    static void Main()
    {
        var settings = new XmlWriterSettings
        {
            Indent = true
        };
        using (var writer = XmlWriter.Create(Console.Out, settings))
        {
            writer.WriteStartElement("Root", ns);
            writer.WriteStartElement("Child");
            writer.WriteAttributeString("attr", "", "val");
            writer.WriteEndElement();
            writer.WriteEndElement();
        }
    }
}

Try like this:

using System;
using System.Text;
using System.Xml;

class Program
{
    private const string ns = "http://tempuri.org/nsA";

    static void Main()
    {
        var settings = new XmlWriterSettings
        {
            Indent = true
        };
        using (var writer = XmlWriter.Create(Console.Out, settings))
        {
            writer.WriteStartElement("Root", ns);
            writer.WriteStartElement("Child");
            writer.WriteAttributeString("attr", "", "val");
            writer.WriteEndElement();
            writer.WriteEndElement();
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文