Click here to Skip to main content
14,699,605 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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);
}


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>


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
Posted
Updated 25-Apr-18 5:25am
v2

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();
}
   
v2
Comments
Maciej Los 24-Apr-18 16:20pm
   
5ed!
Kornfeld Eliyahu Peter 24-Apr-18 16:33pm
   
Thank you...
C. David Johnson 24-Apr-18 18:24pm
   
OK you have changed quite a bit that will make the over all project I have compleatly unmanageable. this is a HUGE convoluted XML structure. there are literally 3752 total file in this one project folder.

When I added the XMLText over the Value/LoanIdentifierBase the serializer error I got was

Cannot serialize object of type 'Enums.LoanIdentifierEnum'. Consider changing type of XmlText member 'V331.Enums.LoanIdentifierEnum.Value' from
V331.EnumBase.LoanIdentifierBase to string or string array.

Because of the structure supplied by the vendor I can not change/refactor the classes. If it were not for the face that the enum value was located in a value position (and named value it most likely would not be a problem

JIC it was confusing before Here is the class

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 Value { set; get; }
}


And the ENUM

public enum LoanIdentifierBase
{
NotSet,
AgencyCase,
InvestorCommitment,
InvestorContract,
InvestorLoan,
InvestorWorkoutCase,
LenderCase,
LenderLoan
}
C. David Johnson 24-Apr-18 18:27pm
   
To give you an idea of the scope of this one project when I used Visual Studios XSD app to convert the XSD to c# objects the single file was 75,000 lines of code and thats just classes and enums
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);


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
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900