zoukankan      html  css  js  c++  java
  • 用DataSet读取xml文件

    假如,有一个这样的xml文件:

    <?xml version="1.0" encoding="UTF-8"?>
    <data>
      <videoType>
        <id>0</id>
        <title>广告集锦</title>
        <picURL>flv/flvImg/MV.jpg</picURL>
        <video>
          <item>
            <id>239</id>
            <title>骏篇</title>
            <picurl>flv/flvImg/用心演绎.jpg</picurl>
            <videoURL>flv/用心演绎.flv</videoURL>
            <downURL>flv/用心演绎.flv</downURL>
            <time>01:01</time>
            <params>239</params>
          </item>
          <item>
            <id>238</id>
            <title>拍摄花絮</title>
            <picurl>flv/flvImg/拍摄花絮.jpg</picurl>
            <videoURL>flv/拍摄花絮.flv</videoURL>
            <downURL>flv/拍摄花絮.flv</downURL>
            <time>04:34</time>
            <params>238</params>
          </item>
        </video>
      </videoType>
      <videoType>
        <id>1</id>
        <title>活动视频</title>
        <picURL>flv/flvImg/V型弯挑战赛.jpg</picURL>
        <video>
          <item>
            <id>243</id>
            <title>2011.04.18</title>
            <picurl>flv/flvImg/首发仪式.jpg</picurl>
            <videoURL>flv/首发仪式.flv</videoURL>
            <downURL>flv/首发仪式.flv</downURL>
            <time>03:36</time>
            <params>243</params>
          </item>
        </video>
      </videoType>
      <videoType>
        <id>2</id>
        <title>品牌视频</title>
        <picURL>flv/flvImg/解读.jpg</picURL>
        <video>
          <item>
            <id>254</id>
            <title>用心幸福可达</title>
            <picurl>flv/flvImg/用心幸福可达.jpg</picurl>
            <videoURL>flv/用心幸福可达.flv</videoURL>
            <downURL>flv/用心幸福可达.flv</downURL>
            <time>03:33</time>
            <params>254</params>
          </item>
          <item>
            <id>253</id>
            <title>一路走来</title>
            <picurl>flv/flvImg/一路走来.jpg</picurl>
            <videoURL>flv/一路走来.flv</videoURL>
            <downURL>flv/一路走来.flv</downURL>
            <time>03:40</time>
            <params>253</params>
          </item>
        </video>
      </videoType>
      <videoType>
        <id>3</id>
        <title>荣誉视频</title>
        <picURL>flv/flvImg/2011CCTV.jpg</picURL>
        <video>
          <item>
            <id>255</id>
            <title>2011CCTV</title>
            <picurl>flv/flvImg/2011CCTV.jpg</picurl>
            <videoURL>flv/2011CCTV.flv</videoURL>
            <downURL>flv/2011CCTV.flv</downURL>
            <time>06:55</time>
            <params>255</params>
          </item>
        </video>
      </videoType>
      <videoType>
        <id>4</id>
        <title>音乐下载</title>
        <picURL>flv/flvImg/高飞.jpg</picURL>
        <video>
          <item>
            <id>256</id>
            <title>版</title>
            <picurl>flv/flvImg/飞.jpg</picurl>
            <videoURL>flv/sound/版.mp3</videoURL>
            <downURL>flv/sound/整版.mp3</downURL>
            <time>04:02</time>
            <params>256</params>
          </item>
        </video>
      </videoType>
    </data> 

    现在要求把这些数据insert到SQL Server数据库中的已存在的表(TableTest)中,然后,你第一眼看到这个要求怎么做?我的第一反应是用代码一个节点一个节点的把数据读出来再放入一个字符串变量中,在用这个变量在数据库中执行…其实,这个方法能实现,不过很麻烦!

    现在,可以用一种相对比较简单的方法:把xml文件读到DataSet中,再在DataSet表中循环读取每个值,放在StringBuilder中,最后再在数据库中执行insert。具体做法是:

    首先,用dataset的ReadXml()方法读取这个xml文件:

    DataSet ds = new DataSet();
    ds.ReadXml(Server.MapPath("../xml/videoList.xml"));

    然后再在这个ds中取到所要用到的表:Tables[0]和Tables[2](你可以把这两个表放在GridView中看看取出来的数据长啥样,方便写sql语句)

    最后用循环读取你所需要的表中的数据(比如我想要的是这些):

    for (int i = 0; i < dt0.Rows.Count; i++)

            {

                for (int j = 0; j < dt2.Rows.Count; j++)

                {

                    if (dt0.Rows[i]["videoType_Id"].ToString() == dt2.Rows[j]["video_Id"].ToString())

                    {

                        //sql脚本

                        StringBuilder InsertSql = new StringBuilder();

                        InsertSql.Append("insert into TableTest(name,adddate,classid,classname,picurl,voidurl,downurl,time) values(");

                        InsertSql.Append("'" + dt2.Rows[j]["title"].ToString() + "','" + DateTime.Now + "','" + Convert.ToInt32(dt0.Rows[i]["videoType_Id"].ToString()) + "','" + dt0.Rows[i]["title"].ToString() + "','");

                        InsertSql.Append(dt2.Rows[j]["picURL"].ToString() + "','" + dt2.Rows[j]["videoURL"].ToString() + "','" + dt2.Rows[j]["videoURL"].ToString() + "','" + dt2.Rows[j]["time"].ToString() + "')");

                        //入库

                        SqlHelper.ExecuteNonQuery(InsertSql.ToString(), null);//需要有SqlHelper文件,这里不列出此文件

                        InsertSql.Remove(0, InsertSql.Length);

                    }

                }

            }

     

    补充:

    DataSet读xml文件是这样存储的:Table(0)中存放Feature层的信息,包括fno等属性,只有一行;
    Table(1)中包括Attribute的信息包括ano等属性,有两行。如果Attribute下还有其他子层,就依次放在 Table(2)..中。 DataSet读取数据之后,可以与DataGrid绑定进行显示,绑定时可以针对DataSet中的单个表,也可以一次绑定所有表。

     

  • 相关阅读:
    成功,离你只有一步之遥
    多灾多难!危机下给企业家们的十大贴心忠告
    《赢道:成功创业者的28条戒律》 简介:
    面对大面积失业潮,普通人有无必要创业?
    赢在行动《越狱》给创业者的启示
    如何优选创业项目、产品和服务
    你是高执行力人才吗?
    创业新闻] 2009年创业者必读的十一本书(转)
    赢在2009 创业者需练好六脉神剑(转)
    相当受用!10个从实质上迅速提升经理人生活的方法
  • 原文地址:https://www.cnblogs.com/huyueping/p/3107735.html
Copyright © 2011-2022 走看看