0

I have been working on deserializing this xml document. I worked with a test document called note.xml which worked perfectly fine. However when I try to deserialize this file, I seem to not return anything. For example:

class Program
{
    static void Main(string[] args)
    {
        try
        {
            string xml_path = "../../Family.xml";
            //Console.WriteLine("Hello");
            XmlSerializer x = new XmlSerializer(typeof(Families));
            Families fam;
            using (XmlReader read = XmlReader.Create(xml_path))
            {
                Console.WriteLine("read: " + read); 
                fam = (Families)x.Deserialize(read);
            }

            Console.WriteLine("This is " + fam);

        }
        catch (Exception e) 
        { 
            Console.WriteLine(e.ToString());
        }
        Console.Read();
    }
}

The code above does return the console.writeline("read: " + read) line but does not display the bottom console.writeline("This is " + fam). When i used Note.xml, it displayed both these lines indicating that the deserialize method worked. However, when i use Family.xml, only the first console.writeline() appears. Is there something i'm missing within my Family.xml that could be causing this to happen?

Here is the Family.xml file:

<?xml version="1.0" encoding="utf-8"?>
<Families>
<Family>
<FamilyID>23654</FamilyID>
<PostalAddress>
  <MailingAddress1>54 The Companionway</MailingAddress1>
  <MailingAddress2>Whitby</MailingAddress2>
  <MailingCity>Porirua</MailingCity>
  <MailingPostCode>5024</MailingPostCode>
</PostalAddress>
<ClientAddress>
  <HomeAddress1>54 The Companionway</HomeAddress1>
  <HomeAddress2>Whitby</HomeAddress2>
  <HomeCity>Porirua</HomeCity>
  <HomePostcode>5024</HomePostcode>
</ClientAddress>
<Family_Client>
  <ClientID>4034</ClientID>
  <Title>Mr</Title>
  <FirstName>Anuj</FirstName>
  <LastName>Hari</LastName>
  <MiddleName>Kumar</MiddleName>
  <PreferredName>Anuj</PreferredName>
  <Email>[email protected]</Email>
  <Gender>Male</Gender>
  <Dob>1992-10-14</Dob>
  <HomePhone>2348070</HomePhone>
  <MobilePhone>0276627137</MobilePhone>
  <BusinessPhone></BusinessPhone>
  <WorkEmail>[email protected]</WorkEmail>
  <Fax></Fax>
  <Smoker>No</Smoker>
  <BestTimeToCall></BestTimeToCall>
  <Occupation></Occupation>
  <Employer></Employer>
  <Industry></Industry>
</Family_Client>
<Loan>
  <LenderName>Abode Mortgages Limited</LenderName>
  <LoanStatus>Conditional Approval</LoanStatus>
  <LoanReferenceNumber>str1234</LoanReferenceNumber>
  <DateCreated>2012-12-13</DateCreated>
  <DateSettled>2012-12-13</DateSettled>
  <DateSubmitted>2012-12-13</DateSubmitted>
  <ApprovalDate>2012-12-13</ApprovalDate>
  <DateDeclined>2012-12-13</DateDeclined>
  <DateWithdrawn>2012-12-13</DateWithdrawn>
  <DatePreApproved>2012-12-13</DatePreApproved>
  <DatePreApprovalExpiry>2012-12-13</DatePreApprovalExpiry>
  <DeferralReviewDate>2012-12-13</DeferralReviewDate>
  <DateCancelled>2012-12-13</DateCancelled>
  <Loan_Structure>
    <LoanTerm>15</LoanTerm>
    <InterestOnlyTerm>0</InterestOnlyTerm>
    <FixedRateTerm></FixedRateTerm>
    <FixedRateExpiryDate>2012-12-13</FixedRateExpiryDate>
    <InterestRate>0.057</InterestRate>
    <Amount>40000</Amount>
    <LoanStructureType>Principal and Interest</LoanStructureType>
    <RateType>Fixed</RateType>
    <FrequencyName>Weekly</FrequencyName>
    <PaymentAmount>500</PaymentAmount>
  </Loan_Structure>
  <Family_Client>
    <ClientID>4034</ClientID>
  </Family_Client>
</Loan>

UPDATE!

After i added Console.WriteLine(e.ToString()). It returned with about 20 lines of errors, the top line saying that the input string was not in the correct format.

The wanted Families class:

[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.17929")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class Families {

private FamiliesFamily[] familyField;

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("Family")]
public FamiliesFamily[] Family {
    get {
        return this.familyField;
    }
    set {
        this.familyField = value;
    }
}
}

And the FamiliesFamily class:

public partial class FamiliesFamily {

private int familyIDField;

private FamiliesFamilyPostalAddress postalAddressField;

private FamiliesFamilyClientAddress clientAddressField;

private FamiliesFamilyFamily_Client[] family_ClientField;

private FamiliesFamilyLoan[] loanField;

/// <remarks/>
public int FamilyID {
    get {
        return this.familyIDField;
    }
    set {
        this.familyIDField = value;
    }
}

/// <remarks/>
public FamiliesFamilyPostalAddress PostalAddress {
    get {
        return this.postalAddressField;
    }
    set {
        this.postalAddressField = value;
    }
}

/// <remarks/>
public FamiliesFamilyClientAddress ClientAddress {
    get {
        return this.clientAddressField;
    }
    set {
        this.clientAddressField = value;
    }
}

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("Family_Client")]
public FamiliesFamilyFamily_Client[] Family_Client {
    get {
        return this.family_ClientField;
    }
    set {
        this.family_ClientField = value;
    }
}

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("Loan")]
public FamiliesFamilyLoan[] Loan {
    get {
        return this.loanField;
    }
    set {
        this.loanField = value;
    }
}
}

enter image description here

10
  • 2
    At least put Console.WriteLine(e.ToString()); inside the catch statement to see what exception is being thrown. That will give you more information on what went wrong while deserializing. Commented Jun 22, 2015 at 21:27
  • sweet i'll try that now and get back to you if it returns something Commented Jun 22, 2015 at 21:32
  • Well that helped haha. It's having trouble turning different formats into string Commented Jun 22, 2015 at 21:34
  • Can you also add the code of FamiliesFamilyPostalAddress, FamiliesFamilyClientAddress, FamiliesFamilyFamily_Client, and FamiliesFamilyLoan class? Commented Jun 22, 2015 at 21:59
  • it probably won't fit. I don't have many characters left. Commented Jun 22, 2015 at 22:10

1 Answer 1

0

So the issue was with in my Family.xml which is set to decimal so it's not allowed to return nothing. Not sure how i managed to overlook something so miniscule, but thanks to ekad who was looking in the right direction.

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

Comments

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.