2014-05-07 27 views
0

這是一個新手問題,但我需要幫助組織我的代碼。這是我的巨大方法。
此方法允許用戶瀏覽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 

      } 
     } 

但是,反序列化paramteter很多不一樣的參數批次在StoreStreamInDB()
如何從deserializationXML()方法中獲取反序列化的列表並將相同的列表傳遞給StoreStreamInDB()

回答

0

您能否從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")); 
} 
相關問題