2

I am going to apologise first off as I have spotted an abundance of questions of this nature already on the site.

I am attempting to Deserialize an XML document into a C# object for later usage but am running into issues.

The error log "There is an error in XML document (30, 14). FormatException: Input string was not in a correct format." insinuates that there is a problem with my document specifically at Line 30 column 14 correct?

(Irrelevant data has been omitted out using the "-" character)

    <?xml version="1.0" encoding="UTF-8"?>
<AddressBook>
    <Contact>
        <Name>Joe Money-Chappelle</Name>
            <ContactType>-----</ContactType>
            <DateofBirth>-----</DateofBirth>
            <AddressLine1>-----</AddressLine1>
            <AddressLine2>-----</AddressLine2>
            <AddressLine3>-----</AddressLine3>
            <AddressLine4 />
            <Postcode>-----</Postcode>
            <Email1>-----</Email1>
            <Email2>-----</Email2>
            <Phone>01883742871</Phone>
            <Mobile>07549893431</Mobile>
            <AdditionalInfo>-----</AdditionalInfo>
    </Contact>
    <Contact>
        <Name>Connor Rice</Name>
            <ContactType>-----</ContactType>
            <DateofBirth>-----</DateofBirth>
            <AddressLine1>-----</AddressLine1>
            <AddressLine2>-----</AddressLine2>
            <AddressLine3>-----</AddressLine3>
            <AddressLine4 />
            <Postcode>-----</Postcode>
            <Email1>-----</Email1>
            <Email2 />
            <Phone />
            <Mobile>07504500881</Mobile>
            <AdditionalInfo>-----</AdditionalInfo>
    </Contact>
</AddressBook>

According to that logic then the line below is the culprit:

<Mobile>07504500881</Mobile>

However it is formatted identically to its counterpart in the section just above it which works fine, as demonstrated below:

<Mobile>07549893431</Mobile>
<Mobile>07504500881</Mobile>

No other lines present with errors (yet...) so I'm not exactly sure where the problem lies in this... If it is blindingly obvious then again I apologize and will happily remove the question if needs be.

9
  • 1
    Maybe the issue is the empty Phone element above? That's the first difference I spot between the two data sets. Could you try filling both entries with dummy data? Commented Apr 17, 2018 at 13:00
  • is the phone number an int or long in your model, then I'd guess some sort of parsing/overflow error as the root cause. Commented Apr 17, 2018 at 13:01
  • It would be awesome if you could provide a minimal reproducible example. Commented Apr 17, 2018 at 13:01
  • @Lennart I will a test run with some dummy data in both elements to see if anything different happens, thanks for the pointer Commented Apr 17, 2018 at 13:02
  • 1
    The line number is not necessarily the lines how you see it... I'd try to reduce the XML blockwise to find where the error is located actually. Quite often one finds invisible(!) unicode characters somewhere between the normal characters. Commented Apr 17, 2018 at 13:05

1 Answer 1

5

The issue is the empty <Phone /> element in the line above the error. Since it is empty, and your Phone type is probably some numerical type, the XML parser tries to parse an empty string (e.g. with Int32.Parse("")) which throws an exception. I don't know which method for deserializing you use, but a nullable type might help.

Sign up to request clarification or add additional context in comments.

1 Comment

Hey I needed to see this, thanks for this answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.