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

2016-02-19 21:44 91 1 收藏

get新技能是需要付出行动的,即使看得再多也还是要动手试一试。今天图老师小编跟大家分享的是从XML中读取数据到内存的实例,一起来学习了解下吧!

【 tulaoshi.com - Web开发 】

  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);

(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/webkaifa/)

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

(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/webkaifa/)

来源:http://www.tulaoshi.com/n/20160219/1627005.html

延伸阅读
标签: Web开发
firefox中JS读取XML文件 在网上搜“firefox中JS读取XML文件”的方法,找了半天,好多都是问了没人答的。看到一堆程序员在抱怨firefox:“除了累死程序员没什么好处。”,言归正传。firefox不支持ie中的ActiveXObject对象,要得到一个XML DOM有以下2种方法: 1、document.implementation.createDocument("", "", null); 2、win...
参考文章:http://chs.gotdotnet.com/quickstart/howto/doc/Xml/XmlNameSpace.aspx 偶的源码:         public void bindXml(string xmlUrl)         {             System...
标签: Web开发
俗话说的好,说起来容易做起来难,放在编程上说是看起来容易编起来难,虽说ajax里面没有什么新的技术,只是思想的转变和旧技术的整合,但是动起手来还是问题多多,首先就是我经常使用的是火狐浏览器,马上就碰到了innerText在火狐里面不被兼容的问题,刚开始找不到原因,后来突然反应过来可能是兼容的问题,百度一下,果不其然,在ff里面要使用t...
标签: Web开发
    计算机世界尽管现在越来越多地试图使用unicode这一世界语来说话,但还是存在着gb2312,shift-jis这样的方言,使用MSXML DOM就会清楚地体会到方言的不便。     我想将下面这个文本文档直接存为转换成xml,是否OK,答案是NG(No Good) strXML="?xml version=""1.0"" encoding=""GB2312""?XML这里是一些编...
标签: Web开发
代码如下: /**   * 得到XML文件属性的集合对象   * @param xmlDoc XML对象   * @param name 属性名称 如: user   * @return 返回 Array 对象   * Example XML:   * ?xml version='...

经验教程

89

收藏

96
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部