在.NET Xml序列化中,是否可以使用基于属性值的不同标签来序列化具有枚举属性的类?

发布于 2024-09-02 09:05:00 字数 1950 浏览 4 评论 0原文

我有一个类,包含一个列表属性,其中列表包含具有枚举属性的对象。

当我序列化它时,它看起来像这样:

<?xml version="1.0" encoding="ibm850"?>
<test>
  <events>
    <test-event type="changing" />
    <test-event type="changed" />
  </events>
</test>

Is it possible, through attribute, orimilar, to get the Xml to Look like this?

<?xml version="1.0" encoding="ibm850"?>
<test>
  <events>
    <changing />
    <changed />
  </events>
</test>

基本上,使用枚举的属性值作为确定标记名称的方法?使用类层次结构(即创建子类而不是使用属性值)是唯一的方法吗?

编辑:经过测试,似乎类层次结构实际上也不起作用。如果有一种方法可以构建类以获得我想要的输出,即使有子类,这也是一个可以接受的答案。

下面是一个示例程序,它将输出上述 Xml(记住在 Visual Studio 中运行时按 Ctrl+F5,否则程序窗口将立即关闭):

using System;
using System.Collections.Generic;
using System.Xml.Serialization;

namespace ConsoleApplication18
{
    public enum TestEventTypes
    {
        [XmlEnum("changing")]
        Changing,

        [XmlEnum("changed")]
        Changed
    }
    [XmlType("test-event")]
    public class TestEvent
    {
        [XmlAttribute("type")]
        public TestEventTypes Type { get; set; }
    }
    [XmlType("test")]
    public class Test
    {
        private List<TestEvent> _Events = new List<TestEvent>();

        [XmlArray("events")]
        public List<TestEvent> Events { get { return _Events; } }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Test test = new Test();
            test.Events.Add(new TestEvent { Type = TestEventTypes.Changing });
            test.Events.Add(new TestEvent { Type = TestEventTypes.Changed });

            XmlSerializer serializer = new XmlSerializer(typeof(Test));
            XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
            ns.Add("", "");
            serializer.Serialize(Console.Out, test, ns);
        }
    }
}

I have a class, containing a list property, where the list contains objects that has an enum property.

When I serialize this, it looks like this:

<?xml version="1.0" encoding="ibm850"?>
<test>
  <events>
    <test-event type="changing" />
    <test-event type="changed" />
  </events>
</test>

Is it possible, through attributes, or similar, to get the Xml to look like this?

<?xml version="1.0" encoding="ibm850"?>
<test>
  <events>
    <changing />
    <changed />
  </events>
</test>

Basically, use the property value of the enum as a way to determine the tag-name? Is using a class hierarchy (ie. creating subclasses instead of using the property value) the only way?

Edit: After testing, it seems even a class-hierarchy won't actually work. If there is a way to structure the classes to get the output I want, even with sub-classes, that is also an acceptable answer.

Here's a sample program that will output the above Xml (remember to hit Ctrl+F5 to run in Visual Studio, otherwise the program window will close immediately):

using System;
using System.Collections.Generic;
using System.Xml.Serialization;

namespace ConsoleApplication18
{
    public enum TestEventTypes
    {
        [XmlEnum("changing")]
        Changing,

        [XmlEnum("changed")]
        Changed
    }
    [XmlType("test-event")]
    public class TestEvent
    {
        [XmlAttribute("type")]
        public TestEventTypes Type { get; set; }
    }
    [XmlType("test")]
    public class Test
    {
        private List<TestEvent> _Events = new List<TestEvent>();

        [XmlArray("events")]
        public List<TestEvent> Events { get { return _Events; } }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Test test = new Test();
            test.Events.Add(new TestEvent { Type = TestEventTypes.Changing });
            test.Events.Add(new TestEvent { Type = TestEventTypes.Changed });

            XmlSerializer serializer = new XmlSerializer(typeof(Test));
            XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
            ns.Add("", "");
            serializer.Serialize(Console.Out, test, ns);
        }
    }
}

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

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

发布评论

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

评论(2

狂之美人 2024-09-09 09:05:00
    public class Test : IXmlSerializable
    {
        private List<TestEvent> _Events = new List<TestEvent>();

        public List<TestEvent> Events { get { return _Events; } }

        #region IXmlSerializable Members

        public System.Xml.Schema.XmlSchema GetSchema()
        {
            return null;
        }

        public void ReadXml(System.Xml.XmlReader reader)
        {
            throw new NotImplementedException();
        }

        public void WriteXml(System.Xml.XmlWriter writer)
        {
            writer.WriteStartElement("events");
            foreach (var item in Events)
            {
                writer.WriteElementString(item.Type.ToString().ToLower(), "");
            }
            writer.WriteEndElement();
        }

        #endregion
    }

如果将 Test 类更改为此,它会产生您想要的输出。唯一的问题是,我认为在重写序列化时不能在 Test 类上使用 XmlType 标记,因此名称将是 Test 而不是 test。

    public class Test : IXmlSerializable
    {
        private List<TestEvent> _Events = new List<TestEvent>();

        public List<TestEvent> Events { get { return _Events; } }

        #region IXmlSerializable Members

        public System.Xml.Schema.XmlSchema GetSchema()
        {
            return null;
        }

        public void ReadXml(System.Xml.XmlReader reader)
        {
            throw new NotImplementedException();
        }

        public void WriteXml(System.Xml.XmlWriter writer)
        {
            writer.WriteStartElement("events");
            foreach (var item in Events)
            {
                writer.WriteElementString(item.Type.ToString().ToLower(), "");
            }
            writer.WriteEndElement();
        }

        #endregion
    }

If you change the Test class to this it produces sort of the output you want. The only problem is that I dont think you can use an XmlType tag on the Test class when overriding seralization so the name will be Test instead of test.

独守阴晴ぅ圆缺 2024-09-09 09:05:00

我认为 XmlType 元标记不可能实现类似的操作。您可能会更幸运地研究 DataContractSerializer 类。您也可以尝试覆盖 OnSerializing 事件,但我认为这不会起作用。

I would assume that something like this is not possible with the the XmlType meta tags. You might have more luck looking into the DataContractSerializer class. You could also try to override the OnSerializing event but I dont think that is going to work.

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