ASP.NET学习之XML操作(2)
作者:admin 日期:2006-10-03
上次我列了四种方法出来,对XML进行读写操作,在同事PYS的帮助下,原来用XMLDOCUMENT也可以OK了,这算是我笨了,我们在CMB这个项目中用这个东东生成一个XML字符串,同时从API中获取相关的XML数据回来,就是用到这个东东,由于相关原因,我只把关键的东东贴出来,其它的东东我就省了,大家对着看就行了,我就不讲太多了:)
主要是两个方法,一个是生成XML,一个是把XML转成我们要的数据类型.这两上都是在逻辑层里实现的.
生成xml:GenerateToXml
///GenerateToXml
///
///doc.OuterXml
private string GenerateToXml(StockOrder order)
{
//Create new xml document
XmlDocument doc = new XmlDocument();
//Create STPAPI_REQUEST node
XmlElement stpapi_requestElement = doc.CreateElement("STPAPI_REQUEST");
stpapi_requestElement.SetAttribute("xmlns", "STPAPI");
//Create ORDERS_AMENDMENT node
XmlElement orders_amendmentElement = doc.CreateElement("ORDERS_AMENDMENT");
orders_amendmentElement.SetAttribute("Type", "Update");
orders_amendmentElement.SetAttribute("ID", "T801");
orders_amendmentElement.SetAttribute("OrderNo", order.OrderNum);
orders_amendmentElement.SetAttribute("Qty", order.OrderQuantity.ToString());
orders_amendmentElement.SetAttribute("Price", order.OrderPrice.ToString());
orders_amendmentElement.SetAttribute("Comment", "");
orders_amendmentElement.SetAttribute("CheckDeviation", "");
orders_amendmentElement.SetAttribute("ConfirmPriceWarning", "");
orders_amendmentElement.SetAttribute("FrontEnd", "0");
orders_amendmentElement.SetAttribute("DoClientSideCheck", "");
orders_amendmentElement.SetAttribute("AmendBy", "");
orders_amendmentElement.InnerText = "";
//Append to xml document
stpapi_requestElement.AppendChild(orders_amendmentElement);
doc.AppendChild(stpapi_requestElement);
//return xml string
return doc.OuterXml;
返回XML:GenerateFromXml
///GenerateFromXml
///
///stockOrder
private StockOrder GenerateFromXml(string strXML)
{
try
{
XmlDocument doc = new XmlDocument();
doc.LoadXml(strXML);
XmlNamespaceManager manager = new XmlNamespaceManager(doc.NameTable);
manager.AddNamespace("STP", "STPAPI");
//XmlNode root = doc.SelectSingleNode("//STP:RETURN", manager);//Select the Return node
XmlNode root = doc.SelectSingleNode(".//RETURN");
if (root != null)
{
//get the order detail after amendment
ORDERS_SINGLE_DETAIL detail = new ORDERS_SINGLE_DETAIL();
stockOrder=detail.Execute(stockOrder);
return stockOrder;//Return normally
}
else
{
容错处理
}
}
catch (Exception e)
{
容错处理
}
主要是两个方法,一个是生成XML,一个是把XML转成我们要的数据类型.这两上都是在逻辑层里实现的.
生成xml:GenerateToXml
///
///
///
private string GenerateToXml(StockOrder order)
{
//Create new xml document
XmlDocument doc = new XmlDocument();
//Create STPAPI_REQUEST node
XmlElement stpapi_requestElement = doc.CreateElement("STPAPI_REQUEST");
stpapi_requestElement.SetAttribute("xmlns", "STPAPI");
//Create ORDERS_AMENDMENT node
XmlElement orders_amendmentElement = doc.CreateElement("ORDERS_AMENDMENT");
orders_amendmentElement.SetAttribute("Type", "Update");
orders_amendmentElement.SetAttribute("ID", "T801");
orders_amendmentElement.SetAttribute("OrderNo", order.OrderNum);
orders_amendmentElement.SetAttribute("Qty", order.OrderQuantity.ToString());
orders_amendmentElement.SetAttribute("Price", order.OrderPrice.ToString());
orders_amendmentElement.SetAttribute("Comment", "");
orders_amendmentElement.SetAttribute("CheckDeviation", "");
orders_amendmentElement.SetAttribute("ConfirmPriceWarning", "");
orders_amendmentElement.SetAttribute("FrontEnd", "0");
orders_amendmentElement.SetAttribute("DoClientSideCheck", "");
orders_amendmentElement.SetAttribute("AmendBy", "");
orders_amendmentElement.InnerText = "";
//Append to xml document
stpapi_requestElement.AppendChild(orders_amendmentElement);
doc.AppendChild(stpapi_requestElement);
//return xml string
return doc.OuterXml;
返回XML:GenerateFromXml
///
///
///
private StockOrder GenerateFromXml(string strXML)
{
try
{
XmlDocument doc = new XmlDocument();
doc.LoadXml(strXML);
XmlNamespaceManager manager = new XmlNamespaceManager(doc.NameTable);
manager.AddNamespace("STP", "STPAPI");
//XmlNode root = doc.SelectSingleNode("//STP:RETURN", manager);//Select the Return node
XmlNode root = doc.SelectSingleNode(".//RETURN");
if (root != null)
{
//get the order detail after amendment
ORDERS_SINGLE_DETAIL detail = new ORDERS_SINGLE_DETAIL();
stockOrder=detail.Execute(stockOrder);
return stockOrder;//Return normally
}
else
{
容错处理
}
}
catch (Exception e)
{
容错处理
}
评论: 0 | 引用: 373 | 查看次数: 21487
发表评论