gpt4 book ai didi

c# - 不区分大小写的反序列化

转载 作者:可可西里 更新时间:2023-11-01 08:41:41 25 4
gpt4 key购买 nike

我有一个 XML 文件,其中

我们已经定义了类来序列化或反序列化 XML。

当我们反序列化时,如果 XML 包含如下所示,其中“type”属性为大写,它会抛出错误,就像 xml(2,2) 中存在这样的错误。

<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
}

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

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

最佳答案

以大写形式定义 DocumentType 枚举值或使用标准适配器属性技巧:

[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) ; }
}

关于c# - 不区分大小写的反序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3975354/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com