首页 .NET/Web ASP.NET动态生成XML文件节点控制等

ASP.NET动态生成XML文件节点控制等

作者:胡同里的砖头 围观群众:1594 更新于:2013-12-19

 

WriteXML类:

using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Text;
using System.Collections.Generic;
using CBD.Bll;
using CBD.Mod;

/// <summary>
///WriteXML 的摘要说明
/// </summary>
public class WriteXML
{
    /// <summary>
    /// 生成文件
    /// </summary>
    /// <param name="val"></param>
    /// <param name="filename"></param>
    public static  void CreateXml(Page p,string val, string filename)
    {
        UnicodeEncoding ucode = new UnicodeEncoding();
        StreamWriter sw = new StreamWriter(p.Server.MapPath(filename));
        sw.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
        sw.WriteLine(val);
        sw.Close();
        sw.Dispose();
    }

    /// <summary>
    /// 转换成XML格式字符串
    /// </summary>
    /// <param name="dt"></param>
    /// <returns></returns>
    public static string DataTableToXml()
    {
        List<Img> imgs = new ImgManager().GetAll(0, 10); //获取数据
        StringBuilder strXml = new StringBuilder();
        strXml.AppendLine("<tu>");

        strXml.AppendLine("<images>");
        for (int i = 0; i < imgs.Count; i++)
        {
            strXml.AppendLine("<image src=" + imgs[i].Img1 + "\" /> ");
        }
        strXml.AppendLine("</images> ");

        strXml.AppendLine("<note>");
        for (int i = 0; i < imgs.Count; i++)
        {
            strXml.AppendLine("<txt nr=\"" + imgs[i].Title + "\" /> ");
        }
        strXml.AppendLine("</note> ");

        strXml.AppendLine("<link>");
        for (int i = 0; i < imgs.Count; i++)
        {
            strXml.AppendLine("<url  site=\"" + imgs[i].Url + "\" />");
        }
        strXml.AppendLine("</link> ");

        strXml.AppendLine("</tu>");
        return strXml.ToString();
    }
}

生成方法:

//生成XML
        StringBuilder strb = new StringBuilder();
        strb.Append(WriteXML.DataTableToXml());
        WriteXML.CreateXml(Page, strb.ToString(), "~/imgs.xml");

生成结果:

<?xml version="1.0" encoding="utf-8"?>
<tu>
<images>
<image src="IndexImg/2011-9/634527379996093750.jpg" />
<image src="IndexImg/2011-9/634509851103906250.jpg" />
<image src="IndexImg/2011-9/634509850949687500.jpg" />
<image src="IndexImg/2011-9/634509850767031250.jpg" />
<image src="IndexImg/2011-9/634509850573125000.jpg" />
</images>
<note>
<txt nr="aa" />
<txt nr="4444" />
<txt nr="33333" />
<txt nr="22222" />
<txt nr="111111" />
</note>
<link>
<url  site="http://" />
<url  site="http://ntcbd.nnu.com.cn" />
<url  site="http://ntcbd.nnu.com.cn" />
<url  site="http://ntcbd.nnu.com.cn" />
<url  site="http://ntcbd.nnu.com.cn" />
</link>
</tu>

 

 

  • 本文标题: ASP.NET动态生成XML文件节点控制等
  • 文章分类:【.NET/Web】
  • 非特殊说明,本文版权归【胡同里的砖头】个人博客 所有,转载请注明出处.
留言评论
站点声明:
1、本站【胡同里的砖头】个人博客,借鉴网上一些博客模板,取其各优点模块自行拼装开发,本博客开发纯属个人爱好。
2、所有笔记提供给广大用户交流使用,可转载,可复制,纯个人开发所遇问题锦集记录使用
Copyright © huzlblog.com All Rights Reserved. 备案号:苏ICP备2021056683号-8