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

C# 如何在另一个方法中引用反序列化列表?,c#,wpf,parameter-passing,C#,Wpf,Parameter Passing,这是一个新手问题,但我需要帮助组织我的代码。这是我的大方法。 此方法允许用户浏览xml文件,然后对其进行反序列化 public void DeSerializationXML(string filePath) { XmlRootAttribute xRoot = new XmlRootAttribute(); xRoot.ElementName = "lot_information"; xRoot.IsNullable = false; // Create an

这是一个新手问题,但我需要帮助组织我的代码。这是我的大方法。
此方法允许用户浏览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();
}
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

            }
        }
存储数据库:

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();
}
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

            }
        }
但是,反序列化的参数批次与
StoreStreamInDB()
中的参数批次不同

如何从
deserializationXML()
方法中获取反序列化列表,并将相同的列表传递给
StoreStreamInDB()

能否从
反序列化XML
返回
洛蒂信息
,并将其作为
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"));
}