所以我有以下方法:XML序列化 - 抛自定义错误
private int? myIntField
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public int? IntField{
get {
return this.myIntField;
}
set {
this.myIntField= value;
}
}
现在,我反序列化从一个帖子XML,如果出于某种原因,我得到一个字符串,如“这里是INT现场:55444" ,而不是55444,我响应得到的错误是:Input string was not in a correct format.
这是不是很具体,特别是考虑到我有一个以上的INT场我需要验证。
本来,我打算是这样的:
private string myIntField
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public int? IntField{
get {
return this.myIntField.CheckValue();
}
set {
this.myIntField= value;
}
}
如果校验值进行try-parse
为Int32,如果失败则返回一个空,并增加了一个错误列表。但是,我似乎无法为生成的类指定这个设置。
有我的方式,如果我在的地方整型,DateTime是否等得到的字符串我可以抛出一个特定的错误?
您应该只对通过序列化创建的数据使用反序列化。在其他情况下,特别是当您必须处理可能的错误时,您应该只是定期解析输入(例如,使用LINQ to XML,XPath等) – eWolf
@Tonnie:您建议的方法看起来像是有效的方法。 “我似乎无法为这些生成的课程设置这个设置”,你的意思是什么。 – Phil
@Tonnie:你也是'post'的意思。一个HTTP POST或博客文章? – Phil