ASP.NET学习之XML操作(2)

上次我列了四种方法出来,对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)
            {
            容错处理
            }



上一篇: 爱情,不是一味地付出
下一篇: 生日送给MM的flash
文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags:
相关日志:
评论: 0 | 引用: 373 | 查看次数: 20847
发表评论
昵 称:
密 码: 游客发言不需要密码.
邮 箱: 邮件地址支持Gravatar头像,邮箱地址不会公开.
网 址: 输入网址便于回访.
内 容:
验证码:
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.
字数限制 30 字 | UBB代码 关闭 | [img]标签 关闭