数据反解析

xml文档

View Code
<?xml version="1.0" encoding="UTF-8"?>

<TestScript name="TestScript"

             

            StationType="ddfd" 

            Type="station">



  <TestItem id="1223"

            testName="cw"

            Name="kk"

            delayTime="1000" byPass="NO"

            failTimes="0" blackBox="false"

            itemCmd="default" tryCount="3" isNeedTest="Y"

           

    <Comms commSName="SendCommon"

            </Command>

    <Comms commName="ReceiveCommon"

          

    </Command>

  </TestItem>

</TestScript

导入文档进行数据反解析

       /// <summary>

        /// 導入信息

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        protected void lbSubmit_Click(object sender, EventArgs e)

        {

            if (!fuScript.HasFile)

            {

                EventPopMessage.ShowPopMessage(this, 1, "提示信息", "请选择文件", Icon_Type.Alert);

                return;

            }

            if (!fuScript.FileName.EndsWith(".xml"))

            {

                EventPopMessage.ShowPopMessage(this, 1, "提示信息", "请选择xml文档(*.xml)", Icon_Type.Alert);

                return;

            }



            string str = HttpContext.Current.Request.PhysicalApplicationPath;

            str += "\\UploadFiles\\" + DateTime.Now.ToString("yyyyMMddhhmmss") + fuScript.FileName;

          

            //判断文件是否存在 存在就删除

            if (File.Exists(str))

            {

                File.Delete(str);

            }

            //否则保存

            fuScript.SaveAs(str);

           string errorStr = String.Empty;



           // XML 数据导入到 DataSet中

            DataSet ds = new DataSet();

            ds.ReadXml(str);

            if (!String.IsNullOrEmpty(errorStr))

            {

                EventPopMessage.ShowPopMessage(this, 1, "提示信息", string.Format("导入失败.<br>" + errorStr), Icon_Type.Alert);

                return;

            }

            AnalyzeXmlData(str);

            //文件写入数据表信息

            cls_TestVersion tcv = new cls_TestVersion();

            tcv.FBUILDERID = Common.Get_UserID;

            tcv.FFILENAME = fuScript.FileName;

            tcv.FFILE = str.ToString();

            tcv.DB_Operate = "INSERT";

            BusinessFacade.VERSIONInsertDeleteUpdate(tcv);

            EventPopMessage.ShowPopMessage(this, 1, "提示信息", string.Format("导入成功"), Icon_Type.Alert);

            return;

        }

  





        /// <summary>

        /// 分析XML数据,执行新增更新操作

        /// </summary>

        /// <param name="path"></param>

        /// <param name="tableName"></param>

        /// <returns></returns>

        public static void AnalyzeXmlData(string str)

        {

            

            int userid= Common.Get_UserID;

            //创建XmlDocument类的实例

            XmlDocument xmlDoc = new XmlDocument();

            //调用XmlDocument类中的Load()方法加载XML文件

            xmlDoc.Load(str);

            //获取父节点

            XmlNodeList nodelist = xmlDoc.GetElementsByTagName("TestScript");



           string typename= nodelist[0].Attributes["Type"].Value.Trim();

           

            XmlNode Parent = xmlDoc.FirstChild.NextSibling;

                                 

            //遍历所有节点

            foreach (XmlNode FNode in Parent.ChildNodes)

            {          

                //信息

                Comms  dc = new Comms();

                dc.TYPENAME = typename;

                dc.MODULENAME= FNode.Attributes["TestName"].Value.Trim();

                dc.COMMSNAME = FNode.Attributes["Name"].Value.Trim();    

                dc.FBUILDERID = userid;



                    //写入到数据库新增或更行操作

                    BusinessFacade.CommsInsert(dc);

                }

            }



复制代码

你可能感兴趣的:(解析)