生成XML文件,通过实体生成XML文件

时间:2022-08-20 12:01:43

实体

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;//引入空间

namespace Gome.OpenJob.Model
{
    /// <summary>
    /// 全量索引文件
    /// 作者:赵亮
    /// 创建时间:2014、04、08
    /// </summary>
    [XmlRoot("root")]
    public class XMLBaiDuFullEntitys
    {
        /// <summary>
        /// feed数据格式的版本号
        /// </summary>
        [XmlElement("version")]
        public string version { get; set; }//没有库里
        /// <summary>
        /// feed数据文件最近修改时间
        /// </summary>
        [XmlElement("modified")]
        public string modifiedDate { get; set; }
        /// <summary>
        /// 结合商家创建的百度推广平台账号
        /// </summary>
        [XmlElement("seller_id")]
        public string seller_Name { get; set; }
        /// <summary>
        /// 商家全量推送过来的所有商品数量
        /// </summary>
        [XmlElement("total")]
        public string totalnumber { get; set; }
        /// <summary>
        /// 商家数据包所在的目录地址
        /// </summary>
        [XmlElement("dir")]
        public string dirItem { get; set; }
        /// <summary>
        /// 全量更新只允许 upload 上架商品,此处一个 outer_id 将对应 1000 个商品
        /// </summary>
        [XmlElement("item_ids")]
        public ItemIds ItemIds { get; set; }
    }
    /// <summary>
    /// 全量更新只允许 upload 上架商品,此处一个 outer_id 将对应 1000 个商品
    /// </summary>

    public class ItemIds
    {
        [XmlElement("outer_id")]
        public List<OutId> outIds { get; set; }
    }

    public class OutId
    {
        [XmlAttribute("action")]
        public string Action { get; set; }

        [XmlText]
        public string OutText { get; set; }
    }
}
************************************

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Gome.OpenJob.Model;
using System.IO;
using System.Xml.Serialization;
using System.Xml;
using System.Configuration;

namespace Gome.OpenJob.Common
{
    /// <summary>
    /// 生成全量
    /// </summary>
    public sealed class GenerateXML
    {
        private static XMLBaiDuFullEntitys m_humanResource = null;
        /// <summary>
        /// 用于生成详细内容
        /// </summary>
        private static XMLBaiDuGoodsDetailTest m_GoodsDetailTestResource = null;
        private GenerateXML() { }

        public static XMLBaiDuFullEntitys Get(string path)
        {
            if (m_humanResource == null)
            {
                FileStream fs = null;
                try
                {
                    XmlSerializer xs = new XmlSerializer(typeof(XMLBaiDuFullEntitys));
                    fs = new FileStream(path, FileMode.Open, FileAccess.Read);
                    m_humanResource = (XMLBaiDuFullEntitys)xs.Deserialize(fs);
                    fs.Close();
                    return m_humanResource;
                }
                catch
                {
                    if (fs != null)
                        fs.Close();
                    throw new Exception("Xml deserialization failed!");
                }

            }
            else
            {
                return m_humanResource;
            }
        }
        //生成全量索引
        public static void Set(string path, XMLBaiDuFullEntitys humanResource)
        {
            if (humanResource == null)
                throw new Exception("Parameter humanResource is null!");
           
            FileStream fs = null;
            try
            {
                XmlSerializer xs = new XmlSerializer(typeof(XMLBaiDuFullEntitys));
                
                string path_file = SystemConfig.GetValue("pathFiles");//Open.config
                //生成文件的路径
                string strPath = path_file + "\\" + path;
                fs = new FileStream(strPath, FileMode.Create, FileAccess.Write);
                xs.Serialize(fs, humanResource);
                
                m_humanResource = null;
                fs.Close();
            }
            catch
            {
                if (fs != null)
                    fs.Close();
                throw new Exception("Xml serialization failed!");
            }
        }
    }
}

***************************

 /// <summary>
        /// 生成全量索引文件
        /// 作者:赵亮
        /// 创建时间:2014、04、08
        /// </summary>
        /// <returns></returns>
        public static int Notation = 0;
        public XMLBaiDuFullEntitys FullAmount()
        {
            XMLBaiDuFullEntitys fu = new XMLBaiDuFullEntitys();
            ItemIds ids = new ItemIds();
            ids.outIds = new List<OutId>();
            fu.ItemIds = ids;
            string sqlnum = "select distinct count(*) from VIEW_CPS v inner join gome_category r on (v.category_id=r.categoryid) where seq=0 and v.state='已上架' and v.list_price is not null and v.list_price !=0";
            DbCommand command = this.GetSqlStringCommand(sqlnum);
            object num = this.ExecuteScalar(command);
            int countnum = int.Parse(num.ToString()) + 1000 - 1;
            string count = Math.Ceiling((double)countnum / (double)1000).ToString();
            Notation = int.Parse(count);

            //实体里传值
            fu.version = "1.0";
            fu.modifiedDate = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
            fu.seller_Name = "国美在线";
            fu.totalnumber = num.ToString();//商品数量
            fu.dirItem = "www.gome.com.cn";

            //int outer_idNum = aa / 1000;
            for (int i = 1; i <= int.Parse(count) - 2; i++)
            {
                OutId outId = new OutId();
                outId.Action = "upload";
                outId.OutText = i.ToString();
                fu.ItemIds.outIds.Add(outId);
            }
            return fu;
        }

***************************

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            BaiDuMicroPurchaseBLL abc = new BaiDuMicroPurchaseBLL();
            abc.baiduFullAmount();
            XMLBaiDuFullEntitys xmlList = abc.baiduFullAmount();
            //全量索引文件名
            string path = SystemConfig.GetValue("FullIndexName");//Open.config
            //调用生成xml文件
            GenerateXML.Set(path, xmlList);
            abc.baiduCommodityInformationFile();
            Console.Read();
        }
    }
}