我有一个XML文件
我们已经定义了类来序列化或反序列化XML。
当我们反序列化时,如果XML包含如下所示“类型“属性是大写的,它的抛出错误就像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) ; }
}
对于属性,您还可以简单地评估“伪造枚举”
public enum RelativeType
{
Mum,
Dad,
Son,
GrandDad,
// ReSharper disable InconsistentNaming
MUM = Mum,
DAD = Dad,
SON = Son,
GRANDDAD = GrandDad
// ReSharper restore InconsistentNaming
}
这适用于XML序列化和反序列化。
序列化使用主要定义,而反序列化可以使用两者。
它有一些副作用,特别是当您或通过Enum.Values或类似枚举时。
但如果你知道自己在做什么就行之有效
我认为简短的答案是否定的,你不能忽视XmlAttributes中的大小写,因为它们区分大小写(见本文) 文章)。这意味着如果您的文档是混合大小写,那么您将遇到许多问题(其中一个是这个问题)。
如果是属性名称 类型 在所有文档以大写形式存储时,您不仅可以更改XmlAttribute以反映它的存储方式,因此将行更改为:
[DescriptionAttribute("The sharepoint's document type.")] [XmlAttribute("**TYPE**")]
public DocumentType Type { get; set; }
或者那不起作用?如果没有,在当前情况下我不确定是否有解决方案。