在.NET Xml序列化中,是否可以使用基于属性值的不同标签来序列化具有枚举属性的类?
我有一个类,包含一个列表属性,其中列表包含具有枚举属性的对象。
当我序列化它时,它看起来像这样:
<?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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果将 Test 类更改为此,它会产生您想要的输出。唯一的问题是,我认为在重写序列化时不能在 Test 类上使用 XmlType 标记,因此名称将是 Test 而不是 test。
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.
我认为 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.