JMC
JMC

Reputation: 47

There is an error in XML document (30, 14) FormatException: Input string was not in a correct format.?

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.

Upvotes: 2

Views: 8445

Answers (1)

Lennart
Lennart

Reputation: 10343

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.

Upvotes: 5

Related Questions