Пример #1
0
        /// <summary>
        /// 递归-获取Part零件节点下的所有Part零件以及DOC文档
        /// </summary>
        /// <param name="partNode">Part零件节点</param>
        /// <returns></returns>
        public RootNewModel.ProductModel.PartModel GetPart(XmlNode partNode)
        {
            //try
            //{
            RootNewModel.ProductModel.PartModel result = new RootNewModel.ProductModel.PartModel();

            XmlNodeList DOCNodes  = GetNodeList(partNode, "DOC");
            XmlNodeList PartNodes = GetNodeList(partNode, "PART");

            result.Code      = GetAttrValue(partNode, "CODE");
            result.Code1     = GetAttrValue(partNode, "CODE1");
            result.Version   = GetAttrValue(partNode, "VERSION");
            result.Name      = GetAttrValue(partNode, "NAME");
            result.Quantity  = GetAttrValue(partNode, "QUANTITY");
            result.Material  = GetAttrValue(partNode, "MATERIAL");
            result.Sigweight = GetAttrValue(partNode, "SIGWEIGHT");
            result.TotWeight = GetAttrValue(partNode, "TOTWEIGHT");
            result.Remark    = GetAttrValue(partNode, "REMARK");
            result.FaHCode   = GetAttrValue(partNode, "FAHCODE");
            result.PartType  = GetAttrValue(partNode, "PARTTYPE");

            foreach (XmlNode item in DOCNodes)
            {
                RootNewModel.ProductModel.DocModel docModel = new RootNewModel.ProductModel.DocModel()
                {
                    Code     = GetAttrValue(item, "CODE"),
                    Code1    = GetAttrValue(item, "CODE1"),
                    Name     = GetAttrValue(item, "NAME"),
                    Version  = GetAttrValue(item, "VERSION"),
                    Gcname   = GetAttrValue(item, "GCNAME"),
                    Page     = GetAttrValue(item, "PAGE"),
                    Totpage  = GetAttrValue(item, "TOTPAGE"),
                    Filename = GetAttrValue(item, "FILENAME")
                };
                result.Doc.Add(docModel);

                docModels.Add(docModel);
            }

            foreach (XmlNode item in PartNodes)
            {
                result.Part.Add(GetPart(item));
            }

            return(result);
            //}
            //catch
            //{
            //    MessageBox.Show("XML文件错误!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            //    btnUpLoad.Enabled = false;
            //    return new RootNewModel.ProductModel.PartModel();
            //}
        }
Пример #2
0
        /// <summary>
        /// 读取XML文件 -- 新建设计项目
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public RootNewModel GetProductDataByLocalXML(string strXmlFilePath)
        {
            try
            {
                //xml文件读取的对象
                RootNewModel result = new RootNewModel();

                //文档对象
                XmlDocument doc = new XmlDocument();

                //加载xml文件
                //doc.Load(strXmlFilePath);

                string strXML     = File.ReadAllText(strXmlFilePath, Encoding.UTF8);
                int    explain_S  = strXML.IndexOf("<?");
                int    explain_E  = strXML.IndexOf("?>");
                string strExplain = string.Empty;
                if (!explain_S.Equals(-1))
                {
                    strExplain = strXML.Substring(explain_S, explain_E + 2);
                    strXML     = strXML.Replace(strExplain, strExplain.ToLower());
                }

                doc.LoadXml(strXML);

                //查root根节点
                XmlNode rootNode = GetNode(doc, "/ROOT", "找不到【ROOT】!");

                //查询项目节点(项目有一个,所以是对象)
                XmlNode projectNode = GetNode(doc, "/ROOT/PROJECT", "找不到【PROJECT】!");

                //保存项目属性信息
                result.Project.Projectid   = GetAttrValue(projectNode, "PROJECTID");
                result.Project.Projectname = GetAttrValue(projectNode, "PROJECTNAME");

                //获取产品节点(产品有多个,所以是集合)
                XmlNodeList ProductNodes = GetNodeList(doc, "/ROOT//PRODUCT", "找不到【PRODUCT】!");

                //遍历产品
                foreach (XmlNode itemA in ProductNodes)
                {
                    //实例化一个产品对象
                    RootNewModel.ProductModel productModel = new RootNewModel.ProductModel();

                    //获取这个产品下的所有DOC文档节点
                    XmlNodeList DOCNodesA = GetNodeList(itemA, "DOC");
                    //获取这个产品下的所有Part零件节点
                    XmlNodeList PartNodesA = GetNodeList(itemA, "PART");

                    //获取产品信息(属性值)
                    productModel.Code      = GetAttrValue(itemA, "CODE");
                    productModel.Code1     = GetAttrValue(itemA, "CODE1");
                    productModel.Version   = GetAttrValue(itemA, "VERSION");
                    productModel.Name      = GetAttrValue(itemA, "NAME");
                    productModel.Quantity  = GetAttrValue(itemA, "QUANTITY");
                    productModel.Material  = GetAttrValue(itemA, "MATERIAL");
                    productModel.Sigweight = GetAttrValue(itemA, "SIGWEIGHT");
                    productModel.TotWeight = GetAttrValue(itemA, "TOTWEIGHT");
                    productModel.Remark    = GetAttrValue(itemA, "REMARK");
                    productModel.FaHCode   = GetAttrValue(itemA, "FAHCODE");

                    //遍历产品文档
                    foreach (XmlNode itemB1 in DOCNodesA)
                    {
                        RootNewModel.ProductModel.DocModel docModel = new RootNewModel.ProductModel.DocModel()
                        {
                            Code     = GetAttrValue(itemB1, "CODE"),
                            Code1    = GetAttrValue(itemB1, "CODE1"),
                            Name     = GetAttrValue(itemB1, "NAME"),
                            Version  = GetAttrValue(itemB1, "VERSION"),
                            Gcname   = GetAttrValue(itemB1, "GCNAME"),
                            Page     = GetAttrValue(itemB1, "PAGE"),
                            Totpage  = GetAttrValue(itemB1, "TOTPAGE"),
                            Filename = GetAttrValue(itemB1, "FILENAME")
                        };
                        productModel.Doc.Add(docModel);

                        docModels.Add(docModel);
                    }

                    //遍历所有一级Part零件
                    foreach (XmlNode itemB2 in PartNodesA)
                    {
                        //当前零件的父级节点,即当前产品节点
                        XmlNode productNode = itemB2.ParentNode;

                        //通过递归方法获取当前零件下的所有Part零件以及DOC文档
                        productModel.Part.Add(GetPart(itemB2));
                    }

                    //将当前产品信息添加到xml对象的产品里
                    result.Product.Add(productModel);
                }

                btnUpLoad.Enabled = true;
                btnUpLoad.Focus();

                return(result);
            }
            catch (Exception ex)
            {
                MessageBox.Show("XML文件错误!\n详情:" + ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                btnUpLoad.Enabled = false;
                docModels.Clear();
                return(new RootNewModel());
            }
        }