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

刘静毛毛

刘静毛毛

2016-02-19 21:44

get新技能是需要付出行动的,即使看得再多也还是要动手试一试。今天图老师小编跟大家分享的是从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);

(本文来源于图老师网站,更多请访问https://m.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;
             
          }
  

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/webkaifa/)
展开更多 50%)
分享

猜你喜欢

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

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

从 XML 到 Java 代码的数据绑定(2):从 XML 数据创建类

Web开发
从 XML 到 Java 代码的数据绑定(2):从 XML 数据创建类

s8lol主宰符文怎么配

英雄联盟 网络游戏
s8lol主宰符文怎么配

读取数据库中数据到数组的类

ASP
读取数据库中数据到数组的类

从 XML 到 Java 代码的数据绑定(1)

Web开发
从 XML 到 Java 代码的数据绑定(1)

lol偷钱流符文搭配推荐

英雄联盟 网络游戏
lol偷钱流符文搭配推荐

从 XML 到 Java 代码的数据绑定(3):从文本到字节码

Web开发
从 XML 到 Java 代码的数据绑定(3):从文本到字节码

用Ajax读取XML格式的数据

Web开发
用Ajax读取XML格式的数据

lolAD刺客新符文搭配推荐

英雄联盟
lolAD刺客新符文搭配推荐

XML数据查询技术已经成为现今的研究热点

XML数据查询技术已经成为现今的研究热点

XML 增、删、改和查示例

XML 增、删、改和查示例
下拉加载更多内容 ↓