这篇文章给大家分享的是有关从XML中如何读取数据到内存的内容。小编觉得挺实用的,因此分享给大家做个参考。一起跟随小编过来看看吧。
从XML中读取数据到内存的实例
public clsSimuResultByOneGoods GetOneGoodsSimulationxml(string PathAndFileName) { clsSimuResultByOneGoods OneGoods = new clsSimuResultByOneGoods();//自己定义的一个类 Hashtable AllLocationResult = new Hashtable(); System.Xml.XmlTextReader r = new XmlTextReader(PathAndFileName); string LocationID = ""; DataTable LocationTable = null; while(r.Read()) { if(r.NodeType == XmlNodeType.Element) { switch(r.LocalName) { case "Result": OneGoods.GoodsCode = r.GetAttribute("GoodsCode"); OneGoods.From = Convert.ToDateTime(r.GetAttribute("FromDate")); OneGoods.To = Convert.ToDateTime(r.GetAttribute("ToDate")); break; case "Location": LocationID = r.GetAttribute("ID"); LocationTable = new DataTable(); LocationTable.Columns.Add("Date",typeof(DateTime)); LocationTable.Columns.Add("SafetyStock",typeof(decimal)); LocationTable.Columns.Add("ForecastDemand",typeof(decimal)); LocationTable.Columns.Add("FinalOutput",typeof(decimal)); LocationTable.Columns.Add("FinalInput",typeof(decimal)); LocationTable.Columns.Add("SimuStock",typeof(decimal)); LocationTable.Columns.Add("SimuStockTime",typeof(decimal)); LocationTable.Columns.Add("ImportWorkDay",typeof(bool)); LocationTable.Columns.Add("ImportWorkDay",typeof(bool)); break; case "Record": if(LocationTable != null) { DataRow dr = LocationTable.NewRow(); dr["Date"] = Convert.ToDateTime(r.GetAttribute("Date")); if(r.GetAttribute("SafetyStock") != null && r.GetAttribute("SafetyStock") != "") dr["SafetyStock"] = Convert.ToDecimal(r.GetAttribute("SafetyStock")); if(r.GetAttribute("ForecastDemand") != null && r.GetAttribute("ForecastDemand") != "") dr["ForecastDemand"] = Convert.ToDecimal(r.GetAttribute("ForecastDemand")); if(r.GetAttribute("FinalInput") != null && r.GetAttribute("FinalInput") != "") dr["FinalInput"] = Convert.ToDecimal(r.GetAttribute("FinalInput")); if(r.GetAttribute("FinalOutput") != null && r.GetAttribute("FinalOutput") != "") dr["FinalOutput"] = Convert.ToDecimal(r.GetAttribute("FinalOutput")); if(r.GetAttribute("SimuStock") != null && r.GetAttribute("SimuStock") != "") dr["SimuStock"] = Convert.ToDecimal(r.GetAttribute("SimuStock")); if(r.GetAttribute("SimuStockTime") != null && r.GetAttribute("SimuStockTime") != "") dr["SimuStockTime"] = Convert.ToDecimal(r.GetAttribute("SimuStockTime")); if(r.GetAttribute("ImportWorkDay") != null && r.GetAttribute("ImportWorkDay") != "") dr["ImportWorkDay"] = Convert.ToBoolean(r.GetAttribute("ImportWorkDay")); if(r.GetAttribute("ExportWorkDay") != null && r.GetAttribute("ExportWorkDay") != "") dr["ExportWorkDay"] = Convert.ToBoolean(r.GetAttribute("ExportWorkDay")); LocationTable.Rows.Add(dr); } break; default: break; } } else if(r.NodeType == XmlNodeType.EndElement) { switch(r.LocalName) { case "Location": if(LocationTable != null) { LocationTable.AcceptChanges(); AllLocationResult.Add(LocationID,LocationTable); LocationID = ""; LocationTable = null; } break; default: break; } } } OneGoods.AllLocationResult = AllLocationResult; return OneGoods; }
感谢各位的阅读!关于从XML中如何读取数据到内存就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到吧!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。