c# xml Attributes[ ].Value 空值 判断

        static private bool parseXmlListInsert(XmlNodeList nodeList, string str)
        {
            try
            {
                if (nodeList != null && nodeList.Count != 0)
                {
                    for (int i = 0; i < nodeList.Count; i++)
                    {
                        string table_Name = string.Empty;
                        if (nodeList[i].Attributes["tableName"] != null)
                        {
                            table_Name = nodeList[i].Attributes["tableName"].Value.Trim();
                            Tool.WriteLogToFile("xml文件中读取的表名:["+table_Name+"]", businessType);
                        }
                        string query_Fields = string.Empty;
                        //xml文件中包含queryFields时,即包含数据库表字段的情况
                        if (nodeList[i].Attributes["queryFields"] != null)
                        {
                            query_Fields = nodeList[i].Attributes["queryFields"].Value.Trim();
                        }
                        if (query_Fields == "*" && table_Name == "T_TEMP")
                        {
                            query_Fields = "CRKZ_GUID,GD_GUID,PM_JZ,SX_SJX,CJGS_ZD_GUID,WGSZD_SM";
                        }
                        if (string.IsNullOrEmpty(table_Name) || string.IsNullOrEmpty(query_Fields))
                        {
                            Tool.WriteLogToFile("parseXmlListInsert 表名或字段系列为空", businessType);
                            return false;
                        }
                        string[] fields = query_Fields.Split(',');
                        if (fields.Length == 0)
                        {
                            Tool.WriteLogToFile("xml文件对应的表没有字段,query_Fields长度为0", businessType);
                            return false;
                        }
                        Tool.WriteLogToFile("解析xml文件包含表名:" + table_Name, businessType);
                        if (nodeList[i].HasChildNodes)
                        {
                            XmlNodeList list = nodeList[i].SelectNodes("RECORD");
                            if (list != null && list.Count != 0)
                            {
                                for (int j = 0; j < list.Count; j++)
                                {
                                    List sList = new List();
                                    foreach (string f in fields)
                                    {
                                        //20190904 bug 修正  该处 f == "GD_GUID" 屏蔽了其他字段值入库,去除了该条件
                                        //修正了字段为空的处理 注释部分为原来的写法
                                        /*if (list[j].Attributes[f] == null)
                                        {
                                            sList.Add("");
                                        }
                                        if (f == "GD_GUID" && list[j].Attributes[f] != null && string.IsNullOrEmpty(list[j].Attributes[f].ToString()))
                                        {
                                            Tool.WriteLogToFile("parseXmlListInsert 主键Gd_Guid为空!", businessType);
                                            sList.Add("");
                                        }
                                        if (!(list[j].Attributes[f] == null) && !(f == "GD_GUID" && list[j].Attributes[f] != null && string.IsNullOrEmpty(list[j].Attributes[f].ToString())))
                                        {
                                            sList.Add(list[j].Attributes[f].Value.Trim());
                                        }*/
                                        if ( list[j].Attributes[f]==null )
                                        {
                                            sList.Add("");
                                        }
                                        else
                                        {
                                            sList.Add(list[j].Attributes[f].Value.Trim());
                                        }
                                    }
                                    if (!Tool.InsertToOracle(query_Fields, sList, table_Name,strCnn))
                                    {
                                        Tool.WriteLogToFile("parseXmlListInsert 插入数据库过程出错,表名:" + table_Name+ "字段:"+query_Fields, businessType);
                                        return false;
                                    }
                                }
                            }
                            else
                            {
                                Tool.WriteLogToFile("parseXmlListInsert 无record子节点", businessType);
                                return false;
                            }
                        }
                        else
                        {
                            Tool.WriteLogToFile("parseXmlListInsert 无子节点", businessType);
                            return false;
                        }
                    }
                    return true;
                }
                else
                {
                    Tool.WriteLogToFile("parseXmlListInsert 无主记录或附记录", businessType);
                    return true;
                }
            }
            catch (System.Exception ex)
            {
                Tool.WriteLogToFile("详细错误信息  parseXmlListInsert 插入异常信息:" + ex.Message, businessType);
                return false;
            }
        }

        #endregion

上文中 query_Fields = “CRKZ_GUID,GD_GUID,PM_JZ,SX_SJX,CJGS_ZD_GUID,WGSZD_SM”;
记得不要写成 query_Fields = “CRKZ_GUID, GD_GUID, PM_JZ, SX_SJX, CJGS_ZD_GUID, WGSZD_SM”;
,号间隔的字段不要加空格 在 文中 if ( list[j].Attributes[f]==null ) 判断 做精确匹配加入空格无法匹配,导致程序添加字段值错误,或丢失。

你可能感兴趣的:(c#)