C#导入Xml文件到Sqlserver

来源:互联网 发布:windows cmd 窗口大小 编辑:程序博客网 时间:2024/06/03 13:29

要导入的Xml文件:

 

<?xml version="1.0" encoding="gb2312"?>
<Tbl_UpdateLogs>
 <Table>
  <id>32</id>
  <title>新增执法机构页面</title>
  <content>qqqqqq</content>
  <module>组织机构</module>
  <updateTime>2009-07-31T00:00:00+08:00</updateTime>
  <operator>王永刚</operator>
 </Table>
 <Table>
  <id>33</id>
  <title>执法人员资格页面</title>
  <content>大幅度放到大幅度放到</content>
  <module>组织机构</module>
  <updateTime>2009-07-29T00:00:00+08:00</updateTime>
  <operator>王永刚</operator>
 </Table>
 <Table>
  <id>34</id>
  <title>111111</title>
  <content>dfdwdd</content>
  <module>qwqwq</module>
  <updateTime>2009-07-29T00:00:00+08:00</updateTime>
  <operator>wyg</operator>
 </Table>
 <Table>
  <id>35</id>
  <title>qq</title>
  <content>fjdldldsss</content>
  <module>qqqqqqq</module>
  <updateTime>2009-07-30T00:00:00+08:00</updateTime>
  <operator>wyg</operator>
  </Table>
 <Table>
  <id>36</id>
  <title>2009222</title>
  <content>ddddd</content>
  <module>22</module>
  <updateTime>2009-07-31T00:00:00+08:00</updateTime>
  <operator>wyg</operator>
 </Table>
 <Table>
  <id>37</id>
  <title>1234455</title>
  <content>ddddddd</content>
  <module>11111</module>
  <updateTime>2009-07-31T00:00:00+08:00</updateTime>
  <operator>wyg</operator>
 </Table>
</Tbl_UpdateLogs>    

 

 

 /// <summary>
      /// 读取Xml文件,获得所有节点的Value值
      /// </summary>
      /// <param name="fileName">Xml文件名</param>
      /// <param name="filePath">存放的路径</param>
      /// <param name="rootName">Xml根节点名称</param>
      /// <returns></returns>
       
        public  void ImportXmlFile(string fileName,string filePath,string rootName)
        {          
           
            string loadPath = HttpContext.Current.Server.MapPath(filePath + fileName);
            try
            {
                XmlDocument xmldoc = new XmlDocument();

                xmldoc.Load(loadPath);

                //获取根节点<rootName>的所有子节点

                XmlNodeList myNodeList = xmldoc.SelectSingleNode(rootName).ChildNodes;

                //遍历<根节点>的所有子节点

                foreach (XmlNode myXmlNode in myNodeList)
                {
                    XmlNodeList subNodeList = myXmlNode.ChildNodes;
                  

                    UpdateLogs updateLog = new UpdateLogs();

                    foreach (XmlNode  subXmlNode in subNodeList)
                    {
                        switch (subXmlNode.Name)
                        {
                            case "id":     //节点的名称,加这个条件是因为<!---->这些节点也会被读出来
                                updateLog.ID = int.Parse(subXmlNode.InnerText.Trim());
                                break;

                            case "title":
                                updateLog.Title = subXmlNode.InnerText.Trim();
                                break;

                            case "content":
                                updateLog.Content = subXmlNode.InnerText.Trim();
                                break;

                            case "module":
                                updateLog.Module = subXmlNode.InnerText.Trim();
                                break;

                            case "updateTime":
                                updateLog.UpdateTime = DateTime.Parse(subXmlNode.InnerText.Trim());
                                break;

                            case "operator":
                                updateLog.Operator = subXmlNode.InnerText.Trim();
                                break;


                            default:
                                break;
                        }
                    }

                    if (isExstsById(updateLog.ID.ToString()))
                    {
                        UpdateLog(updateLog);
                    }
                    else
                    {
                        InsertLog(updateLog);
                    }

                
                }

                File.Delete(loadPath);

                BindData();

                ClientScript.RegisterStartupScript(typeof(string), "importLog", "<script>alert('导入成功!');</script>");


             
            }
            catch (Exception ex)
            {
                ExceptionManager.Handle(ex);
            }

        }

原创粉丝点击