使用XML文件反序列化

时间:2015-06-22 21:21:04

标签: c# xml serialization

我一直在努力反序列化这个xml文档。我使用了一个名为note.xml的测试文档,它工作得很好。但是,当我尝试反序列化此文件时,我似乎没有返回任何内容。例如:

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();
    }
}

上面的代码确实返回了console.writeline("读取:" + read)行,但没有显示底部console.writeline("这是" + fam) 。当我使用Note.xml时,它显示了这两行,表明反序列化方法有效。但是,当我使用Family.xml时,只会出现第一个console.writeline()。我的Family.xml中是否有一些我可能会导致这种情况发生的事情?

以下是Family.xml文件:

<?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>

更新<!/强>

我添加了Console.WriteLine(e.ToString())。它返回了大约20行错误,顶行表示输入字符串的格式不正确。

想要的家庭班:

[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;
    }
}
}

和FamiliesFamily课程:

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

1 个答案:

答案 0 :(得分:0)

所以我的Family.xml中的问题是设置为十进制,因此不允许返回任何内容。不知道我是如何设法忽略这么微不足道的事情,但感谢ekad正朝着正确的方向前进。