DataSet输出指定格式的xml

String sConnection = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["115OLEDB"].ConnectionString; //"Data Source=127.0.0.1;Initial Catalog=testdb;User ID=sa;PassWord=newman2007";

        SqlConnection mySqlConnection = new SqlConnection(sConnection);
        mySqlConnection.Open();
        string sqlstr = "SELECT GoodsID, GdsCode, GdsName, PictureName, Remark, 'Picture' AS Kind,   " +
                      "    './Image/GoodsPicture/' + PictureName+'-1.jpg' AS Source  FROM Esales_GoodsShow";
        SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter(sqlstr, sConnection);
        DataSet myDataSet2 = new DataSet();
        mySqlDataAdapter.Fill(myDataSet2);

        XmlDataDocument xmlDoc;
        xmlDoc = new XmlDataDocument();

        XmlNode xmlDocNode = xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", null);
        xmlDoc.AppendChild(xmlDocNode);

        XmlNode viewer = xmlDoc.CreateElement("Items");
        xmlDoc.AppendChild(viewer);
        DataView dtTemp = (DataView)myDataSet2.Tables[0].DefaultView;
        foreach (DataRowView drProd in dtTemp)
        {
            XmlNode ItemNode = xmlDoc.CreateElement("Item");
            viewer.AppendChild(ItemNode);
            XmlAttribute Name = xmlDoc.CreateAttribute("Name");
            Name.Value = drProd["GdsName"].ToString();
            ItemNode.Attributes.Append(Name);
            XmlAttribute Kind = xmlDoc.CreateAttribute("Kind");
            Kind.Value = "Picture";
            ItemNode.Attributes.Append(Kind);
            XmlAttribute LastModified = xmlDoc.CreateAttribute("LastModified");
            LastModified.Value = drProd["GdsCode"].ToString();
            ItemNode.Attributes.Append(LastModified);
            XmlAttribute Source = xmlDoc.CreateAttribute("Source");
            Source.Value = drProd["Source"].ToString();
            ItemNode.Attributes.Append(Source);
        }
        xmlDoc.Save("e:\\GoodsData.XML");

最后输出的xml格式如下:

<?xml version="1.0" encoding="utf-8" ?>
<Items>
    <Item Name="200支方盒装棉棒" Kind="Picture" LastModified="A-3" Source="./Image/GoodsPicture/A-3.jpg" VideoSource="" />
    <Item Name="两用厕所刷" Kind="Picture" LastModified="SA7814" Source="./Image/GoodsPicture/SA7814.jpg" />
    <Item Name="防滑洗衣刷" Kind="Picture" LastModified="SA7799" Source="./Image/GoodsPicture/SA7799.jpg" />
    <Item Name="餐具卫生箱" Kind="Picture" LastModified="YH5946" Source="./Image/GoodsPicture/YH5946.jpg" />
    <Item Name="80ML调味瓶(小)" Kind="Picture" LastModified="YH5849" Source="./Image/GoodsPicture/YH5849.jpg" />
    <Item Name="玫瑰花纸巾筒" Kind="Picture" LastModified="ZT2334" Source="./Image/GoodsPicture/ZT2334.jpg" />
    <Item Name="动物纸巾筒" Kind="Picture" LastModified="ZT2333" Source="./Image/GoodsPicture/ZT2333.jpg" />
    <Item Name="方形卡通纸巾筒" Kind="Picture" LastModified="ZT2332" Source="./Image/GoodsPicture/ZT2332.jpg" />
</Items>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值