Asp. NET to dynamically generate XML format documents and convert them to HTML files

Source: Internet
Author: User
Tags int size xml reader

Program Implementation function:

1. Read the data, point to the "get" button to dynamically generate XML-formatted documents, and put the generated documents in a TextBox or generate an XML file.

2. Click the "Browse" button to convert the obtained XML format document to HTML and display in the browser.

Firstly, using System.Xml is introduced in the corresponding CS file of aspx.

int m = 0; int loop = 0;

Gets the data used to generate the XML document based on the specified fetch depth depth
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;
}

Click the "Get XML Content" button to perform the event
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);
Gets the data used to generate the XML document based on the specified fetch depth depth
I=initdata (parent, GUID, path, name, cc,depth);
Creating an XML Document Object
Document = new XmlDocument ();
Creating XML node elements
XmlElement channelselement = document. CreateElement ("channels");
Document. AppendChild (channelselement);

for (int k = 1; k <=i; k++)
{
Channelselement.appendchild (document. CreateElement ("CHANNEL"));
}
Get element nodes based on element section names
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]); Set element properties
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;
           //Generating text nodes for corresponding element nodes with initialized data
            Elementnode.appendchild (document. createTextNode (Parent[k]));
      
       }

        XmlNodeList Nodelistguid = document. getElementsByTagName ("GUID");
        int sizeguid = Nodelistguid.count;
        for (int k = 0; k < sizeguid k++)
     & nbsp;  {
            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++)
     & nbsp;  {
            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");//Can be saved as an XML file, located in the C:/icbc/newxml.xml
Create a string for saving content
StringBuilder sb = new StringBuilder ();
The output stream points to a string
XmlWriter writer= xmlwriter.create (SB);
The XML document writes the contents into a string.
Document. WriteContentTo (writer);

Writer. Flush ();
Let the text box in the page display the contents of the XML document
TEXTBOXCONTENT.TEXT=SB. ToString ();


}

Click the "Preview" button to go to the XML-generated HTML page
protected void Buttonyulan_click (object sender, EventArgs e)
{
XmlDocument xml = new XmlDocument ();

if (Textboxcontent.text = null | | Textboxcontent.text = "")
{
Response.Write (' <script>alert (click Get content First ');</script> ");
Return

}
session["xmltextboxcontent"] = Textboxcontent.text;


Response.Write ("<script>window.open (' preexploerxmlmenu.aspx ') </script>");

}

' PreExploerXMLMenu.aspx.CS's contents are as follows

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)
    {
        //Create an instance of the Xmldisplay class
        xmldisplay Xmldisplaydemo = new Xmldisplay () ;
       //Get XML content stating that the string is used to generate StringReader
         string xmlcontent= (String) session["Xmltextboxcontent"];
    
      //Generate position to keep static text on a Web page
         Literal LT = new Literal ();
        LT. Text = xmldisplaydemo.loaddocument (new StringReader (xmlcontent));
        this. PANEL1.CONTROLS.ADD (LT);
   }

This class reads into and processes the XML file
public class Xmldisplay

{

public string Loaddocument (StringReader sr)
{
XmlReader XmlReader = null;

StringBuilder html = new StringBuilder ();
Try
{
Creates an instance of the XmlTextReader.


XmlReader = Xmlreader.create (SR);
Working with XML files
Html. Append (Processxml (XmlReader));
}
catch (XmlException ex)
{
Html. Append ("An XML exception occurs:" + ex.) ToString ());
}
catch (Exception ex)
{
Html. Append ("A normal exception occurs:" + ex.) ToString ());
}
Finally
{
if (XmlReader!= null)
Xmlreader.close ();
}
return HTML. ToString ();
}

Methods for processing XML data to control the format of HTML files generated
private String Processxml (XmlReader XmlReader)
{
StringBuilder temp = new StringBuilder ();
int i = 1;
Temp. Append ("<table border=1>");

XML reader finds tag element for ' NAME '
Xmlreader.readtofollowing ("NAME");
do{
if (i = = 1) temp. Append ("<tr>");
Temp. Append ("<td>");

The position of the XML reader moves forward, pointing to the content of the text element
Xmlreader.read ();
Reading text element content into a string
Temp. Append (Xmlreader.value);
Temp. Append ("</td>");
Display 10 columns of data on one line
if ((i%10==0)) temp. Append ("</tr><tr>");
i++;
while (xmlreader.readtofollowing ("NAME"));


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

}//End Processxml Method
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.