如何解决将xml反序列化为对象时如何忽略异常
我编写了一个 web api,它接受 xml 并转换为 json(特定对象)。
问题说明: 如果 xml 包含错误的数据类型,则抛出异常。
期望的情况:xmlserailizer 应该忽略抛出 execption 的字段。
以下是我的示例输入 xml。
<Invoice>
<ProfileID>bpid:e1212121/ProfileID>
<IssueDate>fault date</IssueDate>
</Invoice>
以下是抛出错误的代码:
using (var stringreader = new StringReader(requestBody))
{
XmlSerializer xmlSerializer = new XmlSerializer(typeof(Invoice));
response = (Invoice)xmlSerializer.Deserialize(stringreader);//this line throws error
}
休耕是我的发票对象
public class invoice
{
private string profileID;
private DateTime _IssueDate;
public string ProfileID
{
get{
return this.profileID;
}
set {
this.profileID = value;
}
}
public DateTime IssueDate
{
get{
return this._IssueDate;
}
set {
this._IssueDate; = value;
}
}
}
总而言之,对于数据类型不匹配的字段,我希望 xmlserialzer 忽略抛出的错误
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。