枚举的enum选择第一个/错误的值 [英] Deserialization of enum selects first / wrong value

查看:92
本文介绍了枚举的enum选择第一个/错误的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用第三方文件,所以我没有设计结构,我坚持使用它。出于某种原因,我的所有枚举都在选择列表中的第一个值而不是正确的值



首先这里是我的反序列化代码



I'm working with a 3rd party file so I did not design the structure, and I'm stuck with it. For some reason all my enums are selecting the first value in the list instead of the correct one

First here is my deserialization code

using (var stream = new FileStream(filetoimport, FileMode.Open))
{
    var ser = new XmlSerializer(typeof(MESSAGE));
    var local_loan = (MESSAGE)ser.Deserialize(stream);
    returned = ToDtax(returned, local_loan);
}





接下来是enum



Next is the enum

[GeneratedCode("xsd", "4.6.81.0")]
[Serializable]
[XmlType(Namespace = "http://www.mismo.org/residential/2009/schemas", AnonymousType = true)]
public enum LoanIdentifierBase
{
    NotSet,
    AgencyCase,
    InvestorCommitment,
    InvestorContract,
    InvestorLoan,
    InvestorWorkoutCase,
    LenderCase,
    LenderLoan,





然后有一个类将枚举作为类的一部分



Then there is the class that has the enum as part of the class

[GeneratedCode("xsd", "4.0.30319.33440")]
[Serializable]
[DebuggerStepThrough]
[DesignerCategory("code")]
[XmlType(Namespace = "http://www.mismo.org/residential/2009/schemas")]
public class LoanIdentifierEnum : BaseClass
{
    [XmlAttribute("SensitiveIndicator")]
    public bool SensitiveIndicator { set; get; }

    [XmlIgnore]
    public bool SensitiveIndicatorSpecified { set; get; }

    [XmlAttribute(Form = XmlSchemaForm.Qualified, Namespace = "http://www.w3.org/1999/xlink", DataType = "NCName")]
    public string label { set; get; }

    [XmlAnyAttribute]
    public XmlAttribute[] AnyAttr { set; get; }

    public LoanIdentifierBase Values { set; get; }





最后,导入文件的部分具有正确的值但是使用错误的值进行反序列化



Finally there is the section of the import file that has the correct values but is deserializing with the wrong values

<LOAN_IDENTIFIERS>
<LOAN_IDENTIFIER SequenceNumber="1">
   <LoanIdentifier SensitiveIndicator="false">UAT0019</LoanIdentifier>
   <LoanIdentifierType SensitiveIndicator="false">LenderCase</LoanIdentifierType>
</LOAN_IDENTIFIER>





我的XML背景非常有限,所以任何帮助都会有很大的帮助



我尝试了什么:



我已将XMLElement和XMLAttribute标记放在枚举上。尝试了在线搜索的几个建议。无论我尝试什么,它总是返回枚举的实际值的第一个条目



I have a very limited background in XML so any help would be greatly appreachiated

What I have tried:

I have put XMLElement and XMLAttribute tags over the enum. tried several suggestions from online searches. No matter what I try it always comes back with the first entry from the enum insted of the actual value

推荐答案

我使用了这组声明并且一切正常:

I used this set of declarations and all works well:
public enum LoanIdentifierBase
{
    NotSet,
    AgencyCase,
    InvestorCommitment,
    InvestorContract,
    InvestorLoan,
    InvestorWorkoutCase,
    LenderCase,
    LenderLoan
}

public class LoanIdentifier
{
    [XmlAttribute()]
    public bool SensitiveIndicator { get; set; }
    [XmlText()]
    public string Value { get; set; }
}

public class LoanIdentifierType
{
    [XmlAttribute()]
    public bool SensitiveIndicator { get; set; }
    [XmlText()]
    public LoanIdentifierBase Value { get; set; }
}

public class LOAN_IDENTIFIER
{
    [XmlAttribute()]
    public int SequenceNumber { get; set; }
    [XmlElement()]
    public LoanIdentifier LoanIdentifier { get; set; }
    [XmlElement()]
    public LoanIdentifierType LoanIdentifierType { get; set; }
}

public class LOAN_IDENTIFIERS
{
    [XmlElement(Type = typeof(LOAN_IDENTIFIER))]
    public ArrayList LOAN_IDENTIFIER = new ArrayList();
}


这是一个STUPID解决方案,但它有效,



This is a STUPID solution but it works,

public LoanIdentifierBase Value { set; get; }





改为





Was changed to

[XmlText]
public string RealValue { set; get; }

public LoanIdentifierBase Value => (LoanIdentifierBase) Enum.Parse(typeof(LoanIdentifierBase), RealValue);





有了这个,RealValue会以正确的值结转并转换为Enum



HAPPY HAPPY JOY JOY我现在可以对其他EnumBase使用的其他实例做同样的事情



With this the RealValue comes over as the correct value and get converted to the Enum

HAPPY HAPPY JOY JOY I now get to do this same thing to over a thousand other instances of other EnumBase useages


这篇关于枚举的enum选择第一个/错误的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆