将xml反序列化为对象时如何忽略异常

时间:2021-04-29 05:57:34

标签: xml-serialization xml-deserialization

我编写了一个 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 忽略抛出的错误

0 个答案:

没有答案
相关问题