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

前端之家收集整理的这篇文章主要介绍了生成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(); } } }

原文链接:https://www.f2er.com/xml/297921.html

猜你在找的XML相关文章