.NET 中的 XmlSerializer 与 XmlSchemaForm.Unqualified

发布于 2024-11-27 13:28:31 字数 1361 浏览 2 评论 0原文

给出以下代码:

using System;
using System.Xml.Schema;
using System.Xml.Serialization;

namespace XmlSerializationTest
{
    [XmlType(Namespace = "http://www.test.com")]
    public class Element
    {
        [XmlElement]
        public int X;
    }

    [XmlRoot(Namespace = "http://www.test.com")]
    public class Root
    {
        [XmlElement(Form = XmlSchemaForm.Unqualified)]
        public Element Element;
    }

    public static class Program
    {
        public static void Main(string[] args)
        {
            var root = new Root { Element = new Element { X = 1 } };
            var xmlSerializer = new XmlSerializer(typeof(Root));
            xmlSerializer.Serialize(Console.Out, root);
        }
    }
}

输出为:

<?xml version="1.0" encoding="ibm852"?>
<Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.test.com">
  <Element xmlns="">
    <X xmlns="http://www.test.com">1</X>
  </Element>
</Root>

问题是为什么将 Form 属性设置为 XmlSchemaForm.Unqualified 会导致 Element 元素的命名空间设置为 " " 即使它具有与 Root 元素具有相同命名空间的 XmlTypeAttribute 属性?

此类代码(XmlSchemaForm.Unqualified 部分)是由 WSCF.blue 工具生成的,它会扰乱命名空间。

Given the following piece of code:

using System;
using System.Xml.Schema;
using System.Xml.Serialization;

namespace XmlSerializationTest
{
    [XmlType(Namespace = "http://www.test.com")]
    public class Element
    {
        [XmlElement]
        public int X;
    }

    [XmlRoot(Namespace = "http://www.test.com")]
    public class Root
    {
        [XmlElement(Form = XmlSchemaForm.Unqualified)]
        public Element Element;
    }

    public static class Program
    {
        public static void Main(string[] args)
        {
            var root = new Root { Element = new Element { X = 1 } };
            var xmlSerializer = new XmlSerializer(typeof(Root));
            xmlSerializer.Serialize(Console.Out, root);
        }
    }
}

the output is:

<?xml version="1.0" encoding="ibm852"?>
<Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.test.com">
  <Element xmlns="">
    <X xmlns="http://www.test.com">1</X>
  </Element>
</Root>

The question is why does setting the Form property to XmlSchemaForm.Unqualified cause the Element element's namespace being set to "" even if it has the XmlTypeAttribute attribute with the same namespace as the Root element?

This kind of code (the XmlSchemaForm.Unqualified part) is generated by the WSCF.blue tool and it's messing up with the namespaces.

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

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

发布评论

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

评论(1

謌踐踏愛綪 2024-12-04 13:28:32

您可以覆盖元素类型中指定的命名空间。例如,您可以有

[XmlElement(Namespace="http://foo.com")]
public Element Element;

并且输出将是

<Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.test.com">
  <Element xmlns="http://foo.com">
    <X xmlns="http://www.test.com">1</X>
  </Element>
</Root>

Microsoft 的 Form = XmlSchemaForm.Unqualified 实现似乎与将 Namespace 设置为 "" 完全等效。特别是,如果您显式指定任何其他命名空间 (MSDN 参考)。如果这样做,您将收到此异常:

未处理的异常:System.InvalidOperationException:存在反映类型“XmlSerializationTest.Root”的错误。 ---> System.InvalidOperationException:反映字段“Element”时发生错误。 ---> System.InvalidOperationException:当存在显式命名空间属性时,Form 属性可能不是“Unqualified”。

You can override namespace specified in the element type. E.g. you can have

[XmlElement(Namespace="http://foo.com")]
public Element Element;

And the output would be

<Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.test.com">
  <Element xmlns="http://foo.com">
    <X xmlns="http://www.test.com">1</X>
  </Element>
</Root>

Microsoft's implementation of Form = XmlSchemaForm.Unqualified appears to be exactly equivalent to setting Namespace to "". In particular, it cannot be used if you explicitly specified any other namespace (MSDN reference). If you do, you will get this exception:

Unhandled Exception: System.InvalidOperationException: There was an error reflecting type 'XmlSerializationTest.Root'. ---> System.InvalidOperationException: There was an error reflecting field 'Element'. ---> System.InvalidOperationException: The Form property may not be 'Unqualified' when an explicit Namespace property is present.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文