不区分大小写的反序列化 [英] Case insensitive Deserialization

查看:42
本文介绍了不区分大小写的反序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个XML文件,其中

I have an XML file where

我们已经定义了用于对XML进行序列化或反序列化的类.

We have defined classes to serialize or deserialize XML.

反序列化时,如果XML包含以下内容,其中" type "属性为大写,则其抛出错误(如xml(2,2)中的错误).

When we deserialize, if the XML contains like below where "type" attribute is in upper case, its throwing error like there is an error in xml(2,2) like that.

<document text="BlankPDF" name="BlankPDF" type="PDF" path="" />

...

[DescriptionAttribute("The sharepoint's document type.")]
[XmlAttribute("type")]
public DocumentType Type
{
    get;
    set;
}

public enum DocumentType
{
    pdf,
    ppt,
    pptx,
    doc,
    docx,
    xlsx,
    xls,
    txt,
    jpg,
    bmp,
    jpeg,
    tiff,
    icon
}

这就是我们定义属性的方式.

this is how we have defined the attribute.

反序列化XML时是否可以忽略大小写?

推荐答案

使用大写字母定义 DocumentType 枚举的值,或使用标准的适配器属性技巧:

Define the values of the DocumentType enumeration in the uppercase or use the standard adaptor property trick:

[Description  ("The sharepoint's document type.")]
[XmlIgnore]
public DocumentType Type { get; set; }

[Browsable    (false)]
[XmlAttribute ("type")]
public string TypeXml
{
    get { return Type.ToString ().ToUpperInvariant () ; }
    set { Type = (DocumentType) Enum.Parse (typeof (DocumentType), value, true) ; }
}

这篇关于不区分大小写的反序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆