简体   繁体   中英

Deserializing with XML File

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>anuj@nzfsg.co.nz</Email>
  <Gender>Male</Gender>
  <Dob>1992-10-14</Dob>
  <HomePhone>2348070</HomePhone>
  <MobilePhone>0276627137</MobilePhone>
  <BusinessPhone></BusinessPhone>
  <WorkEmail>anuj@nzfsg.co.nz</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;
    }
}
}

在此处输入图片说明

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM