我正在尝试像这样创建 xml:
<CreditApplication>
<ApplicantData>
<FirstName> John </FirstName>
<LastName> Smith </LastName>
</ApplicantData>
<CoApplicantData>
<FirstName> Mary </FirstName>
<LastName> Jane </LastName>
</CoApplicantData>
</CreditApplication>
我已经这样定义了我的类:
[XmlRoot("CreditApplication")]
public class CreditApplication
{
[XmlElement("ApplicantData")]
public CreditApplicant Applicant;
[XmlElement("CoApplicantData")]
public CreditApplicant CoApplicant;
}
public class CreditApplicant : INotifyPropertyChanged
{
...
[XmlElement("FirstName")]
public string FirstName { set; get; }
[XmlElement("LastName")]
public string LastName { set; get; }
...
}
在 CreditApplication 类的更下方,我引用了在程序中其他地方定义的枚举,这些枚举也需要可序列化。
当我实际运行程序并尝试使用以下命令对课程进行 serlize 化时:
XmlSerializer applicantXMLSerializer = new XmlSerializer(typeof(CreditApplication));
StringWriter applicantStringWriter = new StringWriter();
XmlWriter applicantXmlWriter = XmlWriter.Create(applicantStringWriter);
applicantXMLSerializer.Serialize(applicantXmlWriter, application);
var applicantXML = applicantStringWriter.ToString();
但我得到了错误:There was an error reflecting type 'Models.Credit.CreditApplication'
有谁知道我做错了什么?
编辑:
我已经更新了上面的代码以反映建议的更改。但是,还有其他问题已经出现。
我有一个这样定义的枚举:
[DataContract]
public enum Relationship
{
Spouse = 4,
ResidesWith = 1,
Parent = 2,
Other = 3,
PersonalGuarantor = 5,
CoApplicant = 6
}
如上所示,零不是一个已定义的选项。因此,没有默认值。我围绕未设置的关系默认为零的想法设计了程序。这样我就可以很容易地查看是否设置了一个值。如果我定义了零,然后将其初始化为“无关系”或类似的东西,那么就无法判断用户是否将值设置为“无关系”,或者他们只是没有选择一个选项。
搬家: