从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
投稿

猜你喜欢

  • 使用python实现mqtt的发布和订阅

    2021-04-16 12:27:53
  • Vue组件通信方法案例总结

    2023-07-02 16:51:58
  • UCDChina.com 关于用户期望的讨论

    2008-07-10 11:55:00
  • Oracle 下医嘱执行函数

    2009-03-02 10:49:00
  • Python实现的生成自我描述脚本分享(很有意思的程序)

    2023-08-14 20:21:06
  • networkx库绘制带权图给无权图加权重输出

    2022-01-29 05:54:23
  • pandas带有重复索引操作方法

    2023-04-19 20:22:12
  • vue-cli3项目配置eslint代码规范的完整步骤

    2024-05-29 22:23:12
  • Go语言实现定时器的方法

    2024-02-09 04:55:21
  • Python使用剪切板的方法

    2022-01-25 02:17:39
  • Python中使用urllib2模块编写爬虫的简单上手示例

    2023-10-18 22:14:31
  • 使用sql语句创建和删除约束示例代码

    2024-01-15 21:55:51
  • 在Java编程中使用正则表达式

    2022-10-20 02:10:38
  • Python的Twisted框架上手前所必须了解的异步编程思想

    2021-05-02 14:21:44
  • python中各种路径设置的方法详解

    2022-12-09 23:58:09
  • [项目布局配置]Nosql与PythonWeb-Flask框架组合

    2022-07-08 19:55:40
  • python进程池实现的多进程文件夹copy器完整示例

    2021-04-27 13:05:26
  • Pytorch 数据加载与数据预处理方式

    2021-06-12 11:07:05
  • 深入探讨:MySQL数据库MyISAM与InnoDB存储引擎的比较

    2024-01-25 17:40:34
  • 详解用Python为直方图绘制拟合曲线的两种方法

    2021-06-15 23:58:08
  • asp之家 网络编程 m.aspxhome.com