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>