[英]How can I parse a deserialized property (during deserialization) using XmlSerializer?
I'm using C#/.NET to deserialize a XML file that looks akin to this: 我正在使用C#/ .NET反序列化类似于此的XML文件:
<?xml version="1.0" encoding="utf-8" ?>
<Books>
<Book
Title="Animal Farm"
PublishedDate="08/17/1945"
/>
... More Book nodes ...
</Books>
My classes, for the deserialized XML, look like: 对于反序列化的XML,我的类看起来像:
[XmlRoot("Books")]
public class BookList
{
// Other code removed for compactness.
[XmlElement("Book")]
public List<Book> Books { get; set; }
}
public class Book
{
// Other code removed for compactness.
[XmlAttribute("Title")]
public string Title { get; set; }
[XmlAttribute("PublishedDate")]
public string PublishedDate { get; set; }
public string Month { get; set; }
public string Year { get; set; }
}
When deserializing, I want to store the Book's PublishedDate attribute value in a Book object's PublishedDate property. 反序列化时,我想将Book的PublishedDate属性值存储在Book对象的PublishedDate属性中。 However, I also want to parse the PublishedDate and assign its Month and Year to those properties. 但是,我还想解析PublishedDate并将其Month和Year分配给这些属性。
Is there a way to parse the PublishedDate property, and populate the Month and Year properties, while deserialization occurs ( without using a backing field)? 有没有办法解析PublishedDate属性,并填充Month和Year属性,同时发生反序列化( 不使用支持字段)? For instance, is there an interface that provides a method for post-processing a deserialized node? 例如,是否有一个接口提供了一个后处理反序列化节点的方法? Is there a XmlSerializer event that fires each time a known object is deserialized? 每次反序列化已知对象时是否会触发XmlSerializer事件? Something else along these lines? 还有其他的东西吗?
Note that I realize the no backing field requirement is arbitrary; 请注意,我意识到无支持字段要求是任意的; I'm just curious to learn if there are other options. 我很想知道是否有其他选择。
Well, one workaround would be 那么,一个解决方法就是
public string Month
{
get
{
return DateTime.Parse(PublishedDate).Month;
}
set
{
PublishedDate = ... // this would be a little more code, but you can also do it
}
}
Year
would be pretty similar. Year
非常相似。
As for that event, only events that the XmlSerializer
class provides are 至于该事件,只有XmlSerializer
类提供的事件
UnknownAttribute
, UnknownElement
, UnknownNode
and UnreferencedObject
. UnknownAttribute
, UnknownElement
, UnknownNode
和UnreferencedObject
。 I guess you would have to create your own XmlSerializer
, which is, by the way, not that much of a work. 我想你必须创建自己的XmlSerializer
,顺便说一句,这不是一项工作。
@Ondrej-Janacek 's anwser is the way to go. @ Ondrej-Janacek的anwser是要走的路。 Just to fulfill your curiosity you could potentially remove the XmlAttribute from the PublishDate property and add another one like so 为了满足您的好奇心,您可以从PublishDate属性中删除XmlAttribute并添加另一个如此
[XmlAnyAttribute]
public XmlAttribute[]XAttributes {get; set;}
This property will be called with all attributes found in the XML that don't correspond to any member. 将使用XML中找到的与任何成员不对应的所有属性调用此属性。 Inside it's setter you can lookup the PublishDate value and assign the values to your PublishDate, Month and Year properties. 在它的setter中,您可以查找PublishDate值并将值分配给PublishDate,Month和Year属性。 Without any backing field . 没有任何支持领域 。
See here for more info on the XmlAnyAttribute. 有关XmlAnyAttribute的更多信息,请参见此处 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.