2

I have written a web api which accepts xml and converts to json (a specific object) .

Problem Statement: If xml contains wrong data type exception is thrown.

Desired situation: xmlserailizer should ignore for the fields where execption is thrown.

following is my sample input xml.

 <Invoice>
  <ProfileID>bpid:e1212121/ProfileID>
  <IssueDate>fault date</IssueDate>
</Invoice>

Following is the code which throws error:

using (var stringreader = new StringReader(requestBody))
{
    XmlSerializer xmlSerializer = new XmlSerializer(typeof(Invoice));

    response = (Invoice)xmlSerializer.Deserialize(stringreader);//this line throws error
}

fallowing is my invoice object

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

In summary I want that xmlserialzer ignores error thrown for the fields where the data type is mismatch

Abi Reddy
  • 79
  • 5

0 Answers0