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.

JMC
  • 47
  • 1
  • 8
  • 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? – Lennart Apr 17 '18 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. – Cee McSharpface Apr 17 '18 at 13:01
  • It would be awesome if you could provide a [mcve]. – mjwills Apr 17 '18 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 – JMC Apr 17 '18 at 13:02
  • @dlatikay the phone number and mobile number are both set to Long – JMC Apr 17 '18 at 13:03
  • 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. – Shnugo Apr 17 '18 at 13:05
  • You really shouldn't be storing phone numbers as `long`. What do you think is going to happen to the leading zero? Phone numbers must be strings. – Matthew Watson Apr 17 '18 at 13:07
  • @MatthewWatson I did not think of that inevitability! I shall change those right now thank you! – JMC Apr 17 '18 at 13:09
  • @Lennart You hit the nail on the head by the way, put some dummy data in and straight away working fine, thank you very much for that and of course a big thank you to the rest of you too. – JMC Apr 17 '18 at 13:11

1 Answers1

4

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.

Lennart
  • 9,657
  • 16
  • 68
  • 84