C # XmlChoiceIdentifier ile seri hale

oy
0

Ben XmlChoiceIdentifier kullanarak C # basit nesne seri deneyin:

public class ChargesConditions
{
    public string supplierBillID;
    public ChargesConditions() { }
}

public class PayersConditions
{
    public string payerIdentifier;
    public PayersConditions() { }
}

[XmlType(IncludeInSchema = false)]
public enum ItemChoiceType
{
    Payers,
    Charges
}

public class Choices
{
    [XmlChoiceIdentifier(ItemType)]
    [XmlElement(PayersConditions, Type = typeof(PayersConditions))]
    [XmlElement(ChargesConditions, Type = typeof(ChargesConditions))]
    public object Choice;

    [XmlIgnore]
    public ItemChoiceType ItemType;
}

class Program
{
    static void Main(string[] args)
    {
        ChargesConditions charge = new ChargesConditions();
        charge.supplierBillID = 123;
        Choices c1 = new Choices();
        c1.ItemType = ItemChoiceType.Charges;
        c1.Choice = charge;

        var serializer = new XmlSerializer(typeof(Choices));
        using (var stream = new FileStream(Choices.xml, FileMode.Create))
            serializer.Serialize(stream, c1);
    }
}

Program hattında ınvalidoperationexception özel üretir

var serializer = new XmlSerializer(typeof(Choices));

Ya benim programda yanlıştır? XmlSerializer (int gibi, dize) sadece basit türlerini kullanabilirsiniz mu?

Oluştur 08/11/2018 saat 11:33
kaynak kullanıcı
Diğer dillerde...                            

Cookies help us deliver our services. By using our services, you agree to our use of cookies. Learn more