XML反序列化 - 自动将属性值转换为类(.net)

时间:2022-04-16 13:24:15

(vb.net/c#/etc)

I am having trouble figuring out how to do a bit of deserialization magic. Currently the standard deserialization works fine, including the enums, but now I want to convert an attribute into a class. Oh! what was I thinking!

我无法弄清楚如何做一些反序列化魔术。目前标准的反序列化工作正常,包括枚举,但现在我想将属性转换为类。哦!我在想什么!

My xml looks a bit like this:

我的xml看起来有点像这样:

....
<review user="..." version="2.2">...</review>

And this for my property/class:

这对我的财产/班级来说:

[XmlAttribute("version")]
public MyVersion Version { get; set; }

class MyVersion  {
    // equality overloaded
    // can ctype() from string to MyVersion
    // constructor that takes a single string, etc
}

How can I help the serializer along, so that it can automatically deserialize my string property into this class? Do I need to modify the MyVersion class in some way, or change the definition of the property?

如何帮助序列化程序,以便它可以自动将我的字符串属性反序列化为此类?我是否需要以某种方式修改MyVersion类,或更改属性的定义?

  • I do not want to have to override any methods like OnDeserialized, etc. It is not worth it for this project.
  • 我不想重写OnDeserialized等任何方法。这个项目不值得。

If this can't be done with the default xml deserializer, then that would be good enough to know. There are lots of things it isn't good for, so I won't be surprised.

如果使用默认的xml反序列化器无法做到这一点,那么这就足以让人知道了。有很多东西是不好的,所以我不会感到惊讶。

Thanks!

2 个解决方案

#1


This is not supported in a declarative way. You will have to implement IXmlSerializable on the parent class (the one that is serialized to an element) and perform the conversion between the string and the MyValue type manually.

声明方式不支持此操作。您必须在父类(序列化为元素的类)上实现IXmlSerializable,并手动执行字符串和MyValue类型之间的转换。

#2


You could do this quite easily - Just not as a deserialisation action.

你可以很容易地做到这一点 - 只是不作为反序列化行动。

Use XSD to create your classes for deserialisation. NOw these are all partial classes so you can write a new part of the review class (that contains the attribute 'version') and add a method that gets/sets the version.

使用XSD创建用于反序列化的类。这些都是部分类,因此您可以编写评论类的新部分(包含属性'version')并添加获取/设置版本的方法。

In the get method simple create a new instance of that class and in the set method simple update the existing version from ther supplied version class.

在get方法中,简单地创建该类的新实例,并在set方法中简单地更新其提供的版本类中的现有版本。

#1


This is not supported in a declarative way. You will have to implement IXmlSerializable on the parent class (the one that is serialized to an element) and perform the conversion between the string and the MyValue type manually.

声明方式不支持此操作。您必须在父类(序列化为元素的类)上实现IXmlSerializable,并手动执行字符串和MyValue类型之间的转换。

#2


You could do this quite easily - Just not as a deserialisation action.

你可以很容易地做到这一点 - 只是不作为反序列化行动。

Use XSD to create your classes for deserialisation. NOw these are all partial classes so you can write a new part of the review class (that contains the attribute 'version') and add a method that gets/sets the version.

使用XSD创建用于反序列化的类。这些都是部分类,因此您可以编写评论类的新部分(包含属性'version')并添加获取/设置版本的方法。

In the get method simple create a new instance of that class and in the set method simple update the existing version from ther supplied version class.

在get方法中,简单地创建该类的新实例,并在set方法中简单地更新其提供的版本类中的现有版本。