如何在另一个方法中引用反序列化列表

本文关键字:引用 反序列化 列表 方法 另一个 | 更新日期: 2023-09-27 18:02:09

这是一个新手问题,但我需要帮助组织我的代码。这是我的大方法。
该方法允许用户浏览xml文件,然后对其进行反序列化。

public void DeSerializationXML(string filePath)
{
    XmlRootAttribute xRoot = new XmlRootAttribute();
    xRoot.ElementName = "lot_information";
    xRoot.IsNullable = false;
    // Create an instance of lotinformation class.
    var lot = new LotInformation();
    // Create an instance of stream writer.
    TextReader txtReader = new StreamReader(filePath);
    // Create and instance of XmlSerializer class.
    XmlSerializer xmlSerializer = new XmlSerializer(typeof (LotInformation), xRoot);
    // DeSerialize from the StreamReader
    lot = (LotInformation) xmlSerializer.Deserialize(txtReader);
    // Close the stream reader
    txtReader.Close();
    //Storing deserialized strings to db
    using (var db = new DMIDataContext())
    {

        LotInformation newLot = new LotInformation();

        if (newLot != null)
        {
            newLot.Id = lot.Id;
            newLot.lot_number = lot.lot_number;
            newLot.exp_date = lot.exp_date;
            LotNumber = newLot.lot_number;
            ExpirationDate = newLot.exp_date.ToString();
            //Grabs the lot_number column from db that is distinct
            var lotNum = db.LotInformation.GroupBy(i => i.lot_number).Select(group => group.FirstOrDefault());
            //Loops through the lot numbers column in db and converts to list 
            foreach (var item in lotNum)
            {
                Console.WriteLine(item.lot_number);
            }
            LotNumList = lotNum.ToList();
            foreach (Components comp in lot.Components)
            {
                newLot.Components.Add(comp);
            }
            ComponentsList = newLot.Components;
            foreach (Families fam in lot.Families)
            {
                newLot.Families.Add(fam);
            }
            FamiliesList = newLot.Families;
            try
            {
                db.LotInformation.Add(newLot);
                db.SaveChanges();
                Console.WriteLine("successfully");
            }
            catch
            {
                //TODO: Add a Dialog Here
            }
        }
    }

现在,我想把这个方法分解成两个或三个方法,以简化和清理代码。

到目前为止我所做的是:

反序列化:

public void DeSerializationXML(string filePath)
{
    XmlRootAttribute xRoot = new XmlRootAttribute();
    xRoot.ElementName = "lot_information";
    xRoot.IsNullable = false;
    // Create an instance of lotinformation class.
    var lot = new LotInformation();
    // Create an instance of stream writer.
    TextReader txtReader = new StreamReader(filePath);
    // Create and instance of XmlSerializer class.
    XmlSerializer xmlSerializer = new XmlSerializer(typeof(LotInformation), xRoot);
    // DeSerialize from the StreamReader
    lot = (LotInformation)xmlSerializer.Deserialize(txtReader);
    // Close the stream reader
    txtReader.Close();
}

StoreInDatabase:

public void StoreStreamInDB(string lot)
{
    //Storing deserialized strings to db
    using (var db = new DMIDataContext())
    {

        LotInformation newLot = new LotInformation();

        if (newLot != null)
        {
            newLot.Id = lot.Id;
            newLot.lot_number = lot.lot_number;
            newLot.exp_date = lot.exp_date;
            LotNumber = newLot.lot_number;
            ExpirationDate = newLot.exp_date.ToString();
            //Grabs the lot_number column from db that is distinct
            var lotNum = db.LotInformation.GroupBy(i => i.lot_number).Select(group => group.FirstOrDefault());
            //Loops through the lot numbers column in db and converts to list 
            foreach (var item in lotNum)
            {
                Console.WriteLine(item.lot_number);
            }
            LotNumList = lotNum.ToList();
            foreach (Components comp in lot.Components)
            {
                newLot.Components.Add(comp);
            }
            ComponentsList = newLot.Components;
            foreach (Families fam in lot.Families)
            {
                newLot.Families.Add(fam);
            }
            FamiliesList = newLot.Families;
            try
            {
                db.LotInformation.Add(newLot);
                db.SaveChanges();
                Console.WriteLine("successfully");
            }
            catch
            {
                //TODO: Add a Dialog Here
            }
        }

然而,反序列化的参数lot与StoreStreamInDB()中的参数lot不相同。
我如何从deserializationXML()方法中获取反序列化列表并将相同的列表传递给StoreStreamInDB() ?

如何在另一个方法中引用反序列化列表

您可以从DeSerializationXML返回LotInformation并将其作为StoreStreamInDB的参数吗?例如…

public static LotInformation ReadLotFromFile(string filePath)
{
    LotInformation lot;
    using (var reader = new StreamReader(filePath))
    {
        XmlSerializer xmlSerializer = new XmlSerializer(typeof(LotInformation), new XmlRootAttribute { ElementName = "lot_information", IsNullable = false });
        lot = (LotInformation)xmlSerializer.Deserialize(reader);
    }
    return lot;
}
public static void AddLotToDb(LotInformation lot)
{
    using(var db = new DMIDataContext())
    {
        db.LotInformation.Add(lot);
        db.SaveChanges();
    }
}
public static void ExampleUsage()
{
    AddLotToDb(ReadLotFromFile("my_lot.xml"));
}