• 生成XML、讀取XML


    生成XML方法:

     XmlDocument xml = new XmlDocument();
                               
                    //插入聲明
                    XmlDeclaration xmlDelaration = xml.CreateXmlDeclaration("1.0", "UTF-8", null);
                    XmlElement root = xml.DocumentElement;
                    xml.InsertBefore(xmlDelaration, root);

                    XmlElement node_Results = xml.CreateElement("Results");
                    xml.AppendChild(node_Results);

     return xml.InnerXml;

    或者:

      XmlDocument xmldoc = new XmlDocument();
                xmldoc.LoadXml("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<Results></Results>");

     XmlNodeList nodeList = xmldoc.GetElementsByTagName("Results");
                        XmlNode node_results = null;
                        if (nodeList.Count > 0)
                        {
                            node_results = nodeList[0];
                        }

    //屬於Attributes添加方法:

    XmlAttribute _ShipLotNo = xml.CreateAttribute("ShipLotNo");
    _ShipLotNo.Value = lot.Info.ssl_iShipLotNo.ToString();
    node_OrderItem.Attributes.Append(_ShipLotNo);

     return xmldoc.InnerXml;

    **************************************************************************

    讀取XML方法:

     private PrePlanReturnData DeserializePrePlanXML(string strXML)
            {
                PrePlanReturnData rtn = new PrePlanReturnData();
                rtn.PEIs = new List<PrePlanReplyPEI>();
                rtn.ShipLots = new List<PrePlanReplyShipLot>();

                XmlDocument xml = new XmlDocument();
                xml.LoadXml(strXML);
                XmlNodeList list = xml.GetElementsByTagName("RETURNINFO");
                if (list != null)
                {
                    foreach (XmlNode node in list)
                    {
                        rtn.ReturnCode = node["RETURNCODE"].InnerText;
                        rtn.ReturnMessagess = node["RETURNMESSAGE"].InnerText;


                        break;
                    }
                }

                list = xml.GetElementsByTagName("RESULTS");
                if (list != null)
                {
                    foreach (XmlNode node in list)
                    {
                        if (node.Attributes["ProjectNo"] != null)
                        {
                            rtn.OrderNo = node.Attributes["ProjectNo"].Value;
                        }
                        if (node.Attributes["ProjectID"] != null)
                        {
                            rtn.OrderID = Convert.ToInt32(node.Attributes["ProjectID"].Value);
                        }

                        if (rtn.OrderNo == "")
                        {
                            if (node.Attributes["SONo"] != null)
                            {
                                rtn.OrderNo = node.Attributes["SONo"].Value;
                            }
                            if (node.Attributes["SOID"] != null)
                            {
                                rtn.OrderID = Convert.ToInt32(node.Attributes["SOID"].Value);
                            }
                        }

                        if (node["ERRORMESSAGE"] != null)
                        {
                            rtn.ReturnMessagess += "\n" + node["ERRORMESSAGE"].InnerText;
                        }

                        if (node["SUGGESTMESSAGE"] != null)
                        {
                            rtn.ReturnMessagess += "\n" + node["SUGGESTMESSAGE"].InnerText;
                        }
                    }
                }

                list = xml.GetElementsByTagName("ShipLot");
                if (list != null)
                {
                    foreach (XmlNode node in list)
                    {
                        PrePlanReplyShipLot shiplot = new PrePlanReplyShipLot();
                        if (node.Attributes["ShipLotNo"] != null)
                        {
                            shiplot.ShipLotID = Convert.ToInt32(node.Attributes["ShipLotNo"].Value);
                        }

                        if (node["SugBeginDate"] != null)
                        {
                            shiplot.SugBeginDate = StringToDateTime(node["SugBeginDate"].InnerText);
                        }
                        if (node["SugFinishDate"] != null)
                        {
                            shiplot.SugFinishDate = StringToDateTime(node["SugFinishDate"].InnerText);
                        }
                        if (node["SUGGESTMESSAGE"] != null)
                        {
                            shiplot.Message = node["SUGGESTMESSAGE"].InnerText;
                        }
                        if (node["ERRORMESSAGE"] != null)
                        {
                            shiplot.Message = node["ERRORMESSAGE"].InnerText;
                        }

                        rtn.ShipLots.Add(shiplot);
                    }
                }

                list = xml.GetElementsByTagName("PEI");
                if (list != null)
                {
                    foreach (XmlNode node in list)
                    {
                        PrePlanReplyPEI pei = new PrePlanReplyPEI();
                        if (node.Attributes["ProductID"] != null)
                        {
                            pei.ProductID = node.Attributes["ProductID"].Value;
                        }
                        if (node.Attributes["EditionCD"] != null)
                        {
                            pei.EditionCD = node.Attributes["EditionCD"].Value;
                        }
                        if (node.Attributes["ImprintCD"] != null)
                        {
                            pei.ImprintCD = node.Attributes["ImprintCD"].Value;
                        }


                        if (node["CPMInDate"] != null)
                        {
                            pei.CPMInDate = StringToDateTime(node["CPMInDate"].InnerText);
                        }

                        rtn.PEIs.Add(pei);
                    }
                }

                return rtn;
            }

  • 相关阅读:
    【python】+json+解析数组json字符串(且键没有引号)(完美解决)
    【Python】+类内部方法相互调用
    【Python】+字符串转换为日期(互转)+获取当前时间+获取当前时间戳
    【python】+tushare库+判断指定日期是否是交易日
    【python】+占位符
    【python】【pycharm】+代码自动提示
    【python】+命名规范
    【python】+'chromedriver' executable needs to be in PATH
    【python】+8大数据类型
    【python】+字典操作(全)
  • 原文地址:https://www.cnblogs.com/guyuehuanhuan/p/1934224.html
Copyright © 2020-2023  润新知