为XMl文档添加子节点,依据淘宝

    protected void Page_Load(object sender, EventArgs e)
    {
        str_product();
    }

    conn co = new conn();
    public void str_product()
    {
        //用SQL语句查询数据并转换成XML文档,保存起来
        string str_pro = "select distinct(product_no),brand,product_type,product_class,price,price2,price4,memo from tbl_product_store where brand='KILLAH'";
        DataSet ds = co.storenumshop(str_pro );
        XmlDataDocument xmlDoc = new XmlDataDocument(ds);            
        xmlDoc.Save( Server.MapPath("data.xml") );//保存。
    }

    /// <summary>
    /// 根据XML文档的货号添加对应的色质码,颜色,库存信息,
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Button1_Click(object sender, EventArgs e)
    {
       
        //string product_no = "YIBT749085";//要查询的id值

        //读取到XML文档
        XmlDocument xml = new XmlDocument();
        xml.Load(Server .MapPath ("data.xml"));
        XmlNodeList xmlTable = xml.SelectNodes("NewDataSet/Table");
        foreach (XmlNode table in xmlTable)
        {
            //取出货号,并根据货号来查询SKU值,每一个颜色,色质码,尺码对应一个SKU
            string product_no = table.SelectSingleNode("product_no").InnerText;
            //当节点是product_no的值是为上面值时,就可以去数据库查询信息
            if (table.SelectSingleNode("product_no").InnerText == product_no)
            {
                //查询数据库信息
                string str_sku = "select color,product_size,color2,number2,number from tbl_product_store where product_no='" + product_no + "'";
                DataSet dr_sku = co.storenumshop (str_sku);
                XmlNode skus = table.SelectSingleNode("skus");

                if (skus == null)//如果没有,是新创建
                {
                    skus = xml.CreateElement("skus");
                }

                int row_count = dr_sku.Tables[0].Rows.Count;

                //以行数进行循环读取每一行每一个SKU值
                for (int i = 0; i < row_count; i++)
                {
                    //生成SKU节点,并插入到XMl文档里面
                    string color =dr_sku.Tables [0].Rows [i]["color"].ToString ();//绑定第一个字段 
                    string product_size = dr_sku.Tables[0].Rows[i]["product_size"].ToString();
                    string color2 = dr_sku.Tables[0].Rows[i]["color2"].ToString();
                    string number = dr_sku.Tables[0].Rows[i]["number"].ToString();
                    string number2 = dr_sku.Tables[0].Rows[i]["number2"].ToString();

                    XmlElement sku = xml.CreateElement("sku");

                    XmlElement sku_created = xml.CreateElement("color");
                    sku_created.InnerText = color;
                    sku.AppendChild(sku_created);

                    XmlElement sku_created2 = xml.CreateElement("product_size");
                    sku_created2.InnerText = product_size;
                    sku.AppendChild(sku_created2);

                    XmlElement sku_created3 = xml.CreateElement("color2");
                    sku_created3.InnerText = color2;
                    sku.AppendChild(sku_created3);

                    XmlElement sku_created4 = xml.CreateElement("number");
                    sku_created4.InnerText = number.ToString();
                    sku.AppendChild(sku_created4);

                    XmlElement sku_created5 = xml.CreateElement("number2");
                    sku_created5.InnerText = number2.ToString();
                    sku.AppendChild(sku_created5);
                    skus.AppendChild(sku);

                }

                //根节点添加SKU节点
                table.AppendChild(skus);

            }
           
        }
        //保存XML文档 
        xml.Save(Server .MapPath ("data2.xml"));
    }
posted @ 2010-11-19 14:26  小锋神  阅读(1169)  评论(0编辑  收藏  举报