如何在 xml 中的 c# 中反序列化后创建对象

How to create an object after deserialization in c# in xml

我想在反序列化后创建一个对象,但我不想序列化该对象。 这是一个例子:

public class Visit : INotifyPropertyChanged
{
    private int id;
    private int serialNumber;
    private DateTime dateOfVisit;
    private VisitTime visitTime;
    private VisitType visitType;
    private int price;
    private VisitStatus visitStatus;
    private PaymentMethod paymentMethod;
    private String cause;
    private String recommendation;
    private Prescription prescription;
    private Patient patient;
    [NonSerialized]
    private Doctor doctor;
    private Room room;

    // more code below...
}

我不想连载医生,我的医生在完全不同的 xml 文件中。我正在序列化 doctorID 属性 很好。在对我已经序列化的属性进行反序列化之后,我想创建一个 Doctor class 的新实例并将其分配给 Visit class 中的 doctor 字段。我怎样才能正确地做到这一点?

这是我正在序列化的xml:

<?xml version="1.0" encoding="utf-8"?>
<ArrayOfVisit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <Visit>
    <Id>1</Id>
    <SerialNumber>1</SerialNumber>
    <VisitTime>
        <StartTime>2021-05-02T09:00:00</StartTime>
        <EndTime>2021-05-02T10:00:00</EndTime>
    </VisitTime>
    <DateOfVisitString>1. Jan 00:00 AM</DateOfVisitString>
    <DateOfVisit>0001-01-01T00:00:00</DateOfVisit>
    <VisitType>examination</VisitType>
    <VisitTypeString>Pregled</VisitTypeString>
    <Price>10</Price>
    <PriceString>10$</PriceString>
    <VisitStatus>forthcoming</VisitStatus>
    <VisitStatusString>Predstojeći</VisitStatusString>
    <DoctorName>Janko Radovic</DoctorName>
    <DoctorId>12123131</DoctorId>
    <PaymentMethod>digitalWallet</PaymentMethod>
    </Visit>
</ArrayOfVisit>

我尝试使用 onDeserializing() 和 onDeserialized() 方法,但是当数据绑定到带有 DoctorName 属性 的 View 时,我得到一个空指针异常。它只是说医生是空的。

[System.Xml.Serialization.XmlIgnore]
public Doctor Doctor
{
    get
    {
        return doctor;
    }
    set
    {
        doctor = value;
        OnPropertyChanged("Doctor");
        OnPropertyChanged("DoctorName");
        OnPropertyChanged("DoctorId");
    }
}

public String DoctorName
{
    get
    {
        return doctor.Name + " " + doctor.Surname;
    }
    set
    {

    }
}

public String DoctorId
{
    get
    {
        return doctor.Id;
    }
    set
    {

    }
}

[OnDeserialized()]
internal void OnDeserializedMethod(StreamingContext context)
{
    DoctorService doctorService = new DoctorService();
    doctor = doctorService.getDoctorById(DoctorId);
}

这是我序列化和反序列化的方式:

public void SerializeObject<T>(T serializableObject, string fileName)
{
    if (serializableObject == null) { return; }
    XmlSerializer serializer = new XmlSerializer(serializableObject.GetType());
    try
    {
        var temp = AppDomain.CurrentDomain.BaseDirectory;
        temp = Path.Combine(temp.Substring(0, temp.Length - 10), "Storage\");
        fileName = Path.Combine(temp, fileName);
        StreamWriter w = new StreamWriter(fileName);
        serializer.Serialize(w, serializableObject);
        w.Close();
    }
    catch (Exception e)
    {
        //Log exception here
    }
}

public T DeSerializeObject<T>(string fileName)
{
    var temp = AppDomain.CurrentDomain.BaseDirectory;
    temp = Path.Combine(temp.Substring(0, temp.Length - 10), "Storage\");
    fileName = Path.Combine(temp, fileName);
    if (string.IsNullOrEmpty(fileName)) { return default(T); }
    T objectOut = default(T);

    try
    {
        Type outType = typeof(T);
        XmlSerializer serializer = new XmlSerializer(outType);

        StreamReader r = new StreamReader(fileName);
        objectOut = (T)serializer.Deserialize(r);
        r.Close();

    }
    catch (Exception e)
    {
        //Log exception here
    }

    return objectOut;
}

有什么建议吗?只需序列化 id,然后使用该 id 在 Visit class 中创建一个 Doctor 对象?谢谢!

所以根据 mm8 的说法,她提出了一个可行的方法,这就是应该如何完成的。 XMLSerializer 使用模型 class 的获取和设置属性。因此,当序列化一个特定的 class 对象时,序列化的工作方式是它使用 属性 的 get 方法来序列化。要反序列化,它使用 set 方法再次设置已序列化的 class 对象的字段。

如果您不想序列化 class 的特定字段,您应该将 [System.Xml.Serialization.XmlIgnore] 设置在 属性 之上。我只想序列化一个 Doctor class 的 id,我通过 属性:

public String DoctorId
    {
        get
        {
            return doctor.Id;
        }
        set
        {
            DoctorService doctorService = new DoctorService();
            doctor = doctorService.getDoctorById(value);
        }

}

当调用反序列化一个set方法时value等于序列化后的医生id。然后使用从其他存储库查找医生的服务,并在 Visit class.

中设置医生字段