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

实体

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(); } } }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念
xml文件介绍及使用
xml编程(一)-xml语法
XML文件结构和基本语法
第2章 包装类
XML入门的常见问题(二)
Java对象的强、软、弱和虚引用
JS解析XML文件和XML字符串详解
java中枚举的详细使用介绍
了解Xml格式
XML入门的常见问题(四)
深入SQLite多线程的使用总结详解
PlayFramework完整实现一个APP(一)
XML和YAML的使用方法
XML轻松学习总节篇