这是一个新手问题,但我需要帮助组织我的代码。这是我的巨大方法 此方法允许用户浏览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
}
}
但是,反序列化的参数批次与StoreStreamInDB()
中的参数批次不同。
如何从deserializationXML()
方法中获取反序列化列表并将相同列表传递给StoreStreamInDB()
?
答案 0 :(得分:0)
您是否可以从LotInformation
返回DeSerializationXML
并将其作为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"));
}