using System;
using System.Xml;
using System.Globalization;
public class XmlTool{
public static void Main(string[] args){
XML.WriteXMLMain();
}
}
class XML{
#region 时间|通道号|串联单文件名称|终端号
public static string dString_yyyy_MM_dd; //日期表达式:2009_01_01
public static string dString_MM_dd; //日期表达式:01_01
public static string dString_MMdd; //日期表达式:0101
public static string TongDaoNo;
public static string xmlFileName;
public static string ZhongDuanNo;
public static XmlElement col,col1,col2,col3,col4,col5,col6,col7; //标题列
public static XmlElement listheader,lists,list;
public static XmlDocument doc;
public static XmlElement record,listitem,listitem1,listitem2,listitem3,listitem4,listitem5,listitem6,listitem7;
//public static int minTimeItem,maxTimeItem;
#endregion
///写串联单记录record
public static void WriteXMLMain(){
MakeXMLName();
DateTime d=new DateTime();
DateTime myD;//时段
long ssLiuShuiNo=1000000000;//素材编号计数
long myTicks=Environment.TickCount;//流水号
#region 定义root 不动头 lists list
doc=new XmlDocument();
//定义XML 标头信息
try{
XmlDeclaration dec=doc.CreateXmlDeclaration("1.0","GBK",null);
doc.AppendChild(dec);}
catch(Exception e){
Console.WriteLine("->Catch Exception:"+e);
}
lists=doc.CreateElement("lists");
doc.AppendChild(lists);
list=doc.CreateElement("list");
lists.AppendChild(list);
#endregion
AddHeader();//表头listheader
Console.WriteLine("请输入素材的播放时长:");
int perTime=Int32.Parse(Console.ReadLine());//播放时长
int ssNo=0;
Console.WriteLine("请输入素材前缀:");
string ssNameHead=Console.ReadLine();
Console.WriteLine("请输入素材类型 <文本5 图片3 视频1>");
string ssType=Console.ReadLine();
Console.WriteLine("请输入串联单编号:1位递增");
string seriCount=Console.ReadLine();
Console.WriteLine("请输入人员编号:");
string person=Console.ReadLine();
string dateSingle=dString_MMdd;
Console.WriteLine("请输入是否全屏幕 0 非全屏 1 全屏");
string ScreenType=Console.ReadLine();
#region 内容
for(int i=0;i<48;i++){
//修改时段-------------半个小时一个时段30‘
myD=d.AddMinutes(i*30);//时段递增30分钟
string timeStampNo=(i+1).ToString();
string TimeItem=myD.ToString("T", DateTimeFormatInfo.InvariantInfo);//转换为 短时间格式T
for(int j=1;j<=30*60/perTime;j++){//------------修改素材播放时长----------------
#region 批量素材段内顺序 格式:00001
string ssName="";
string ssOrder="";
if(j<10)
ssOrder="0000"+j;
if(j>=10 && j<=99)
ssOrder="000"+j;
if(j>=100 && j<=999)
ssOrder="00"+j.ToString();
if(j>=1000 && j<=9999)
ssOrder="0"+j;
if(j>9999)
ssOrder=j.ToString();
#endregion
//批量处理素材名称:图片00001 00001为素材段内顺序 图片 (1).txt
//ssName="图片"+ssLiuShuiNo.ToString().Substring(5);
int a=ssNo++;
ssName=ssNameHead+" ("+a+")";//----------修改素材版本-----------
//ssName="Sax"+a;
if(a==4){
ssNo=0;
}
#region 动态生成XML节点 record
record=doc.CreateElement("record");
listitem=doc.CreateElement("listitem");//时段编码
listitem.SetAttribute("value",timeStampNo);
record.AppendChild(listitem);
listitem1=doc.CreateElement("listitem");//时段预播时间
listitem1.SetAttribute("value",TimeItem);
record.AppendChild(listitem1);
listitem3=doc.CreateElement("listitem");//素材编号 P22250831 1_ _ _ _ _ 文本5 图片3 视频1
listitem3.SetAttribute("value","P"+person+person+person+ssType+dateSingle+seriCount+ssLiuShuiNo.ToString().Substring(5,5));
record.AppendChild(listitem3);
ssLiuShuiNo++;
if(ssName==ssNameHead+" (4)"){
ssLiuShuiNo=1000000000;}
//每40个素材 轮播一次
listitem2=doc.CreateElement("listitem");//广告版本
listitem2.SetAttribute("value",ssName);
record.AppendChild(listitem2);
listitem4=doc.CreateElement("listitem");//广告长度
listitem4.SetAttribute("value",perTime.ToString());
record.AppendChild(listitem4);
listitem5=doc.CreateElement("listitem");//段内顺序 001
listitem5.SetAttribute("value",ssOrder.Substring(2));
record.AppendChild(listitem5);
listitem6=doc.CreateElement("listitem");//流水号 文本0 图片1 视频2
//listitem6.SetAttribute("value",person+seriCount+dateSingle.Substring(1,3)+ssType+myLiuShui++.ToString().Substring(5,5));
listitem6.SetAttribute("value",String.Format("{0:D11}",myTicks++));
record.AppendChild(listitem6);
listitem7=doc.CreateElement("listitem");//是否全屏幕
listitem7.SetAttribute("value",ScreenType);
record.AppendChild(listitem7);
list.AppendChild(record);
#endregion
}
}
SaveXML();
#endregion
}
///产生XML文件名称
public static void MakeXMLName(){
Console.WriteLine("--------------------------------串联单制造 DEMO V1.0------");
Console.Write("日期:");
DateTime dNow=DateTime.Now;
dString_yyyy_MM_dd=string.Format("{0:yyyy_MM_dd}",dNow);
dString_MM_dd=string.Format("{0:MM_dd}",dNow);
dString_MMdd=string.Format("{0:MMdd}",dNow);
Console.WriteLine(dString_yyyy_MM_dd);
Console.WriteLine("请输入通道号:");
TongDaoNo=Console.ReadLine();//通道号
Console.WriteLine(":"+TongDaoNo);
Console.WriteLine("请输入终端号:");
ZhongDuanNo=Console.ReadLine();//终端号
if(ZhongDuanNo=="") //没有终端号
xmlFileName="Bill_"+dString_yyyy_MM_dd+"_001_"+TongDaoNo+".xml";//产生串联单文件名称
else//有终端号
xmlFileName="Bill_"+dString_yyyy_MM_dd+"_001_"+TongDaoNo+"_"+ZhongDuanNo+".xml";//产生串联单文件名称
//Console.WriteLine(xmlFileName);
}
///增加表头列:listHeader
public static void AddHeader(){
listheader=doc.CreateElement("listheader");
list.AppendChild(listheader);
col=doc.CreateElement("col");
col.SetAttribute("name","时段编号");
listheader.AppendChild(col);
col1=doc.CreateElement("col");
col1.SetAttribute("name","时段预播时间");
listheader.AppendChild(col1);
col2=doc.CreateElement("col");
col2.SetAttribute("name","素材编号");
listheader.AppendChild(col2);
col3=doc.CreateElement("col");
col3.SetAttribute("name","广告版本");
listheader.AppendChild(col3);
col4=doc.CreateElement("col");
col4.SetAttribute("name","广告长度");
listheader.AppendChild(col4);
col5=doc.CreateElement("col");
col5.SetAttribute("name","段内顺序");
listheader.AppendChild(col5);
col6=doc.CreateElement("col");
col6.SetAttribute("name","流水号");
listheader.AppendChild(col6);
col7=doc.CreateElement("col");
col7.SetAttribute("name","是否全屏幕");
listheader.AppendChild(col7);
}
///保存XML文件
public static void SaveXML(){
doc.Save(@"D:\myBill\"+xmlFileName); // 保存文件
Console.WriteLine("创建完成:"+xmlFileName+" <<<"+DateTime.Now.ToString()+">>>");
}
}
主要方法有:
col2=doc.CreateElement("col");
col6.SetAttribute("name","流水号");
listheader.AppendChild(col6);