相忘于江湖

不抛弃,不放弃... 请给我勇敢,改变可以改变的;请给我坚强,接受不可以改变的;请给我智慧,分辨这两者。

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

作者: Maco  
该文章转载自网络大本营

 

程序实现功能:

1.读取数据,点解“获取”按钮动态生成XML格式的文档,并把生成的文档放在TEXTBOX或者生成一个XML文件。

2.点击“浏览”按钮,把获取到的XML格式文档,转换成HTML,在浏览器中显示。

 

首先在ASPX对应的CS文件中引入using System.Xml;

 int m = 0;  int loop = 0;

//根据指定的获取深度DEPTH获取用于生成XML文档的数据
    private int initData(string[] parent, string[] guid, string[] path, string[] name, ChannelCollection cc,int depth)
    {
       
     
        foreach (Channel channel in cc)
        {
          
            parent[m] = channel.Parent.Guid;
            guid[m] = channel.Guid;
            path[m] = channel.Path;
            name[m] = channel.Name;
            m = m + 1;
            if (channel.Channels.Count!=0 &&loop<depth)
            {
                ChannelCollection c = channel.Channels;

                ++loop;
                initData( parent, guid, path, name, c,depth);

            }
  
        }
       return m;
    }

 //点击“获取XML内容“按钮执行的事件
    protected void ButtonGetXml_Click(object sender, EventArgs e)
    {

        ChannelCollection cc = selectedChannel.Channels;
        string[] parent =new string[1000];
        string[] guid = new string[1000];
        string[] path = new string[1000];
        string[] style = new string[1000];
        string[] name = new string[1000];
        int i=0;

        int depth = int.Parse( DropDownListGetDepth.SelectedValue);
        //根据指定的获取深度DEPTH获取用于生成XML文档的数据
        i=initData(parent, guid, path, name, cc,depth);
        //创建XML 文档对象
       document = new XmlDocument();
       //创建XML节点元素
        XmlElement channelsElement = document.CreateElement("Channels");
        document.AppendChild(channelsElement);

        for (int k = 1; k <=i; k++)
        {
            channelsElement.AppendChild(document.CreateElement("CHANNEL"));
        }
        //根据元素节点名获取元素节点
        XmlNodeList nodelist = document.GetElementsByTagName("CHANNEL");
        int size = nodelist.Count;
        for (int k = 0; k < size; k++)
        {
            XmlNode node = nodelist.Item(k);
     
            XmlElement elementNode = (XmlElement)node;
            //elementNode.SetAttribute("guid",guid[k]);设置元素属性
            elementNode.AppendChild(document.CreateElement("PARENT"));
            elementNode.AppendChild(document.CreateElement("GUID"));
            elementNode.AppendChild(document.CreateElement("PATH"));
            elementNode.AppendChild(document.CreateElement("NAME"));
        }

        XmlNodeList nodelistParent = document.GetElementsByTagName("PARENT");
        int sizeParent = nodelistParent.Count;
        for (int k = 0; k < sizeParent; k++)
        {
            XmlNode node = nodelistParent.Item(k);
        
            XmlElement elementNode = (XmlElement)node;
            //用初始化后的数据生成对应元素节点的文本节点
           elementNode.AppendChild(document.CreateTextNode(parent[k]));
      
        }

        XmlNodeList nodelistGuid = document.GetElementsByTagName("GUID");
        int sizeGuid = nodelistGuid.Count;
        for (int k = 0; k < sizeGuid; k++)
        {
            XmlNode node = nodelistGuid.Item(k);
       
            XmlElement elementNode = (XmlElement)node;
            elementNode.AppendChild(document.CreateTextNode(guid[k]));

        }
        XmlNodeList nodelistPath = document.GetElementsByTagName("PATH");
        int sizePath = nodelistPath.Count;
        for (int k = 0; k < sizePath; k++)
        {
            XmlNode node = nodelistPath.Item(k);
      
            XmlElement elementNode = (XmlElement)node;
            elementNode.AppendChild(document.CreateTextNode(path[k]));

        }
        XmlNodeList nodelistName = document.GetElementsByTagName("NAME");
        int sizeName = nodelistName.Count;
        for (int k = 0; k < sizeName; k++)
        {
            XmlNode node = nodelistName.Item(k);
         
            XmlElement elementNode = (XmlElement)node;
            elementNode.AppendChild(document.CreateTextNode(name[k]));

        }

       // document.Save("/icbc/newxml.xml");//可以保存为XML文件,位置在C:/icbc/newxml.xml
       //创建用于保存内容的字符串里
        StringBuilder sb = new StringBuilder();
        //输出流指向字符串
        XmlWriter writer= XmlWriter.Create(sb);
        //XML文档把内容写到字符串里
        document.WriteContentTo(writer);
      
        writer.Flush();
        //让页面中的文本框显示XML文档内容
        TextBoxContent.Text=sb.ToString() ;

      
    }

//点击”预览“按钮,转到XML生成HTML的页面
    protected void ButtonYuLAN_Click(object sender, EventArgs e)
    {
        //XmlDocument xml = new XmlDocument();

        if (TextBoxContent.Text == null || TextBoxContent.Text == "")
        {
            Response.Write("<script>alert('请先点击获取内容');</script>");
            return;

        }
        Session["xmlTextBoxContent"] = TextBoxContent.Text;
      
      
        Response.Write("<script>window.open('PreExploerXMLMenu.aspx')</script>");
     
    }

//'PreExploerXMLMenu.aspx.CS的内容如下

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.IO;
using System.Text;

public partial class Module_ChannelInfoManage_PreExploerXMLMenu : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //创建XmlDisplay类的实例
        XmlDisplay XmlDisplayDemo = new XmlDisplay();
        //得到XML内容,申明为字符串用于生成 StringReader
        string xmlcontent=(string )Session["xmlTextBoxContent"];
    
       //生成在网页上保留静态文本的位置
        Literal LT = new Literal();
        LT.Text = XmlDisplayDemo.LoadDocument(new StringReader(xmlcontent));
        this.Panel1.Controls.Add(LT);
    }

    //这个类读入并处理XML文件
    public class XmlDisplay

    {

        public string LoadDocument(StringReader sr)
        {
            XmlReader xmlReader = null;
         
            StringBuilder html = new StringBuilder();
            try
            {
                //创建XMLTextReader的实例。

            
                xmlReader = XmlReader.Create(sr);
                //处理XML文件
                html.Append(ProcessXml(xmlReader));
            }
            catch (XmlException ex)
            {
                html.Append("发生一个XML异常:" + ex.ToString());
            }
            catch (Exception ex)
            {
                html.Append("发生一个普通异常:" + ex.ToString());
            }
            finally
            {
                if (xmlReader != null)
                    xmlReader.Close();
            }
            return html.ToString();
        }

        //处理XML数据的方法,用于控制生成HTML文件的格式
        private string ProcessXml(XmlReader xmlReader)
        {
            StringBuilder temp = new StringBuilder();
            int i = 1;
            temp.Append("<table border=1>");
        
            //XML读取器找到”NAME“的标记元素
            xmlReader.ReadToFollowing("NAME");
            do{
                if (i == 1) temp.Append("<tr>");
                temp.Append("<td>");

                //xml读取器的位置向前移动,指到 文本元素内容
                xmlReader.Read();
                 //读取文本元素内容到字符串中    
                temp.Append(xmlReader.Value);
                temp.Append("</td>");
                //一行显示10列数据
                if ((i%10==0)) temp.Append("</tr><tr>");
                i++;
            } while (xmlReader.ReadToFollowing("NAME"));


            temp.Append("</tr></table>");
            return temp.ToString();

        }//结束ProcessXML方法
    }
}

 

posted on 2010-07-16 20:33  playman0211  阅读(370)  评论(0编辑  收藏  举报