XML读取数据到内存_Xml教程

编辑Tag赚U币
教程Tag:暂无Tag,欢迎添加,赚取U币!

推荐:诠释XML
XML即可扩展标记语言(eXtensible Markup Language)。标记是指计算机所能理解的信息符号,通过此种标记,计算机之间可以处理包含各种信息的文章等。如何定义这些标记,既可以选择国际通用的标

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

}

分享:怎样利用XMLBean轻松读写XML
一、关于XML解析 XML在Java应用程序里变得越来越重要, 广泛应用于数据存储和交换. 比如我们常见的配置文件,都是以XML方式存储的. XML还应用于Java Message Service和Web Services等技术作为数

来源:模板无忧//所属分类:Xml教程/更新时间:2009-03-15
相关Xml教程