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()
?