简体   繁体   English

如何使用XmlSerializer解析反序列化属性(在反序列化期间)?

[英]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 . UnknownAttributeUnknownElementUnknownNodeUnreferencedObject 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.

相关问题 XmlSerializer反序列化期间如何忽略只读属性集? - How ignore read only property set during XmlSerializer deserialization? 反序列化期间解析JSON属性 - Parse JSON property during deserialization 如何使用XmlSerializer属性解析此XML? - How can I parse this XML with XmlSerializer attributes? 如何使用 JsonSerializerSettings 在反序列化期间忽略属性 - How to ignore property during deserialization using JsonSerializerSettings 我可以让 XmlSerializer 在反序列化时忽略命名空间吗? - Can I make XmlSerializer ignore the namespace on deserialization? 如何使用XmlSerializer在C#中反序列化期间将xml属性转换为自定义对象 - How to convert xml attribute to custom object during deserialization in C# using XmlSerializer 如何通过使用WCF SoapClient在反序列化(XmlSerializer)上忽略UnknownElements? - How to ignore UnknownElements on deserialization (XmlSerializer) by using a WCF SoapClient? 如何在 json 反序列化期间忽略未知的枚举值? - How can I ignore unknown enum values during json deserialization? 可以使用相同的反序列化调用对具有相同属性的2种不同数据类型的JSON对象进行反序列化吗? - Can a JSON object with 2 different data types for the same property be deserialized with the same deserialization call? 如何使用XmlSerializer将异构子节点反序列化为集合? - How can I deserialize heterogeneous child nodes into a collection, using XmlSerializer?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM