您的位置:首页 > Web前端 > CSS

用xsl样式转换xml,生成html

2006-06-02 00:18 363 查看
先手动生成xml

private void Button1_Click(object sender, System.EventArgs e)
{
//创建新的xml
XmlDocument doc = new XmlDocument();
doc.LoadXml("<company></company>");
//设置版本信息
XmlDeclaration xmldecl;
xmldecl = doc.CreateXmlDeclaration("1.0",null,null);
xmldecl.Encoding="gb2312";
//xmldecl.Standalone="yes";

//
XmlElement root = doc.DocumentElement;
doc.InsertBefore(xmldecl, root);
//设置根结点
XmlElement newCompany = doc.DocumentElement;
//创建新的name
XmlElement newName = doc.CreateElement("name");
newName.InnerText = "公司名称"; //公司名称
//加入父结点
newCompany.AppendChild(newName);

XmlElement newInfo = doc.CreateElement("info");
newInfo.InnerText = "简介"; //简介
newCompany.AppendChild(newInfo);

XmlElement newContactinfo = doc.CreateElement("contactinfo");
newContactinfo.InnerText = "网址"; //网址
newCompany.AppendChild(newContactinfo);

XmlElement newContactperson = doc.CreateElement("contactperson");
newContactperson.InnerText = "姓名"; //姓名
newCompany.AppendChild(newContactperson);

XmlElement newContactzip = doc.CreateElement("contactzip");
newContactzip.InnerText = "邮编"; //邮编
newCompany.AppendChild( newContactzip );

XmlElement newContactadd = doc.CreateElement("contactadd");
newContactadd.InnerText = "地址"; //地址
newCompany.AppendChild( newContactadd );

//工作列表
//先创建jobs类表

XmlElement newJobs = doc.CreateElement("jobs");
newCompany.AppendChild( newJobs );

//DataSet ds = new DataSet();
//if(ds!=null)
//{
//foreach(DataRow dr in ds.Tables[0].Rows)
for(int i=0;i<5;i++)
{
XmlElement newJob = doc.CreateElement("job");
newJobs.AppendChild( newJob );

XmlElement newTitle = doc.CreateElement("title");
newTitle.InnerText = i.ToString(); //职位名称
newJob.AppendChild( newTitle );

XmlElement newUrl = doc.CreateElement("url");
newUrl.InnerText = "http://www.020job.com"; //网址
newJob.AppendChild( newUrl );
}
//}

//doc.DocumentElement.AppendChild(newCompany);

XmlTextWriter tr = new XmlTextWriter(Server.MapPath(Random_str()),System.Text.Encoding.GetEncoding("gb2312"));
doc.WriteContentTo(tr);
tr.Close();
}

private string Random_str()
{
Random oRan = new System.Random();
string fileName =
DateTime.Now.Year.ToString() +
DateTime.Now.Month.ToString() +
DateTime.Now.Day.ToString() +
DateTime.Now.Hour.ToString() +
DateTime.Now.Minute.ToString() +
DateTime.Now.Second.ToString() +
oRan.Next(9999).ToString() +
".xml";

return fileName ;
}

后用创造好的xsl转换html
XPathDocument doc = new XPathDocument(Server.MapPath("tem\\XMLFile1.xml" ));
//创建一个新的转换
XslTransform transForm = new XslTransform();
transForm.Load(Server.MapPath("tem\\XSLTFile1.xsl" ));
//输出
FileStream fs = new FileStream(Server.MapPath(Random_str()),FileMode.Create);
XPathNavigator nav = doc.CreateNavigator();
transForm.Transform(nav,null,fs);
fs.Close();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: