从XML中读取数据到内存的实例

来源:互联网 时间:2008-09-04 14:43:00 

从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;
           
        }


标签:
0
投稿

猜你喜欢

  • 在ASP编程中nothing代表什么意思?

    2011-04-15 10:47:00
  • sqlserver中去除字符串中连续的分隔符的sql语句

    2012-06-06 20:07:39
  • JavaScript基础知识篇-你真的了解JavaScript吗?

    2009-09-17 13:00:00
  • 再论Javascript的类继承

    2010-06-26 12:48:00
  • 带进度条的ASP无组件断点续传大文件下载

    2010-06-25 18:27:00
  • 利用XML实现通用WEB报表打印实际使用中的例子

    2008-09-04 14:42:00
  • MS SQL7.0的数据迁移到MySQL上的一种方法

    2008-11-01 16:59:00
  • MySQL表设计优化与索引 (七)

    2010-10-25 20:06:00
  • SQL SERVER的字段类型介绍

    2008-05-23 13:27:00
  • CSS Hack经验总结

    2008-05-01 13:13:00
  • 发散后的期望

    2008-07-31 18:32:00
  • JS 中如何判断 null

    2007-12-13 20:28:00
  • 解决MySQL数据库中与优化器有关的问题

    2008-12-17 16:18:00
  • 利用ASP在线维护数据库

    2007-10-12 13:53:00
  • 社会化音乐:在夹缝中生出花来

    2009-08-24 12:37:00
  • asp中常用的字符串安全处理函数集合(过滤特殊字符等)

    2011-02-20 10:40:00
  • asp使用XMLHTTP下载远程数据输出到浏览器

    2007-11-04 10:34:00
  • asp获取文件md5值

    2008-10-13 09:10:00
  • 使用php数据缓存技术提高执行效率

    2023-05-24 23:14:24
  • 使用XML技术上传文件的例子

    2008-05-29 11:33:00
  • asp之家 网络编程 m.aspxhome.com