從XML讀取資料到記憶體的實例

黄舟
發布: 2017-02-28 16:56:23
原創
1546 人瀏覽過

從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讀取資料到記憶體的實例

rrreee

 以上就是從XML讀取資料到記憶體的實例的內容,更多相關內容請關注PHP中文網(www.php.cn)!

##########
相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!