Dataset outputs XML in the specified format

Source: Internet
Author: User
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");

The output XML format is as follows:

<? XML version = "1.0" encoding = "UTF-8"?> <Items> <item name = "200 left-side boxed cotton swab" 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 =" anti-skid laundry brush "kind =" picture "lastmodified =" sa7799 "Source = ". /image/goodspicture/sa7799.jpg "/> <item name =" "kind =" picture "lastmodified =" yh5946 "Source = ". /image/goodspicture/yh5946.jpg "/> <item name =" 80 ml seasoning bottle (small) "kind =" picture "lastmodified =" yh5849 "Source = ". /image/goodspicture/yh5849.jpg "/> <item name =" Rose tissue cartridge "kind =" picture "lastmodified =" zt2334 "Source = ". /image/goodspicture/zt2334.jpg "/> <item name =" animal tissue cartridge "kind =" picture "lastmodified =" zt2333 "Source = ". /image/goodspicture/zt2333.jpg "/> <item name =" square cartoon tissue cartridge "kind =" picture "lastmodified =" zt2332 "Source = ". /image/goodspicture/zt2332.jpg "/> </items>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.