程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> ASP編程 >> ASP技巧 >> ASP.NET中的XML表單控件

ASP.NET中的XML表單控件

編輯:ASP技巧

導 讀:介紹了有關ASP.Net中XML控件的使用,有個小BUG:在WEBFORM.ASPX中出現的XML控件,其中的transformsource屬性設定了樣式表文件路徑,可是在文章出處沒有找到這個XSL文件.:( 自己解決吧.
在這個代碼中揭示了微軟在ASP.Net架構中隱藏的一個WEB表單控件,即<ASP:XML runat=server/>,我只給代碼,不給解釋,大家自己下課後去研究吧。
另外,由於是beta1,在這個控件中你使用的xslt裡面不能使用<xsl:sort>,當然,亦不能使用那個order-by了,因為它支持的xsl空間是帶"1999"的那個,而不是原來的那個。
另外,我從微軟得到的回答就是在beta2裡面,它將支持<xsl:sort>,就可以全部轉向XML+xsl了,而不用再為源代碼保密問題頭疼了。
請看下例:
webform2.cs
                                -
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.sessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Text;
using System.IO;
using System.XML;

public class WebForm2 : Page
{
    public StringBuilder outputQ;
    public StringBuilder outputXML;
      public DocumentNavigator nav = null;
    public HtmlInputFile XMLFile;
   
    public System.Web.UI.WebControls.Xml MyXML;

    public System.Web.UI.WebControls.TextBox TextBox1;       
    public System.Web.UI.WebControls.TextBox TextBox2;
    public System.Web.UI.WebControls.TextBox TextBox3;       
    public System.Web.UI.WebControls.Button Query;
    public System.Web.UI.WebControls.Label FileLabel;
 
    public void On_KeyUp(object sender, System.EventArgs e)
    {
        Response.Write("Works");
    }

    PRotected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //
            // Evals true first time browser hits the page
            //
        }
    }

    public void Query_Click(object sender, System.EventArgs e)
    {
        HttpPostedFile xmlfile = XMLFile.PostedFile;
         XmlDocument doc = new XMLDocument();   
         MyXml.Document = new XMLDocument();   
//        TextBox2.Text="";
//        TextBox3.Text="";

        if (XMLfile.FileName != String.Empty)
        {
            try
            {
                FileLabel.Text= XMLfile.FileName;

                MyXml.Document.Load(XMLfile.FileName);
                outputXML = new StringBuilder();
                XmlTextReader reader = new XmlTextReader (XMLfile.FileName);
                ShowDocument();
                TextBox3.Text = outputXML.ToString();

                outputQ = new StringBuilder();
                doc.Load(XMLfile.FileName);
                DocumentNavigator nav = new DocumentNavigator(doc);
                // Perform the query e.g. "descendant::book/price"
                XPathQuery(nav, TextBox1.Text);
                TextBox2.Text = outputQ.ToString();

            }
            catch (Exception exp) {
                //outputQ.Append("</xmp><font color=\"#FF6600\">"+ exp.Message+"</font><xmp>");
            }
            finally {}
        }
        else if (FileLabel.Text != String.Empty)
        {
            try
            {
                MyXML.Document.Load(FileLabel.Text);
                outputXML = new StringBuilder();
                XmlTextReader reader = new XMLTextReader (FileLabel.Text);
                ShowDocument();
                TextBox3.Text = outputXML.ToString();

                ShowDocument();

                outputQ = new StringBuilder();
                doc.Load(FileLabel.Text);
                DocumentNavigator nav = new DocumentNavigator(doc);
                // Perform the query e.g. "descendant::book/price"
                XPathQuery(nav, TextBox1.Text);
                TextBox2.Text = outputQ.ToString();

            }
            catch (Exception exp) {
                outputQ.Append("</xmp><font color=\"#FF6600\">"+ exp.Message+"</font><xmp>");
            }
            finally {}
        }
    }

    private void XPathQuery(XMLNavigator navigator, String xpathexpr )
    {
        try
        {
            // Save context node position
            navigator.PushPosition();
            navigator.Select (xpathexpr);
            FormatXML(navigator);

            // Restore context node position
            navigator.PopPosition();       
        }
        catch (Exception e)
        {
        }
    }
   
//***************************** Navigator ************************************
    private void FormatXml (XMLNavigator navigator)
    {
        while (navigator.MoveToNextSelected())
        {
            switch (navigator.NodeType)
            {
            case XMLNodeType.ProcessingInstruction:
                Format (navigator, "ProcessingInstruction");
                break;
            case XMLNodeType.DocumentType:
                Format (navigator, "DocumentType");
                break;
            case XMLNodeType.Document:
                Format (navigator, "Document");
                break;
            case XMLNodeType.Comment:
                Format (navigator, "Comment");
                break;
            case XMLNodeType.Element:
                Format (navigator, "Element");
                break;
            case XMLNodeType.Text:
                Format (navigator, "Text");
                break;
            case XMLNodeType.Whitespace:
                Format (navigator, "Whitespace");
                break;
            }
        }
        outputQ.Append("\r\n");
    }

    // Format the output
    private void Format (XMLNavigator navigator, String NodeType)
    {
        String value = String.Empty;
        String name = String.Empty;

        if (navigator.HasChildren)
        {
            name = navigator.Name;
            navigator.MoveToFirstChild();
            if (navigator.HasValue)
            {
                value = navigator.Value;
            }
        }
        else
        {
            if (navigator.HasValue)
            {
                value = navigator.Value;
                name = navigator.Name;
            }
        }
        outputQ.Append(NodeType + "<" + name + ">" + value);
        outputQ.Append("\r\n");
    }

// ********************************** XMLReader *****************************
    public void ShowDocument ()
    {
        outputXML = new StringBuilder();
        XmlTextReader reader = new XMLTextReader (FileLabel.Text);

        while (reader.Read())
        {
            switch (reader.NodeType)
            {
            case XMLNodeType.ProcessingInstruction:
                Format (reader, "ProcessingInstruction");
                break;
            case XMLNodeType.DocumentType:
                Format (reader, "DocumentType");
                break;
            case XMLNodeType.Comment:
                Format (reader, "Comment");
                break;
            case XMLNodeType.Element:
                Format (reader, "Element");
                break;
            case XMLNodeType.Text:
                Format (reader, "Text");
                break;
            case XMLNodeType.Whitespace:
                break;
            }
        }
        TextBox3.Text = outputXML.ToString();
    }

    protected void Format(XMLReader reader, String NodeType)
    {
        // Format the output
        for (int i=0; i < reader.Depth; i++)
        {
            outputXML.Append('\t');
        }

        outputXML.Append(reader.Prefix + NodeType + "<" + reader.Name + ">" + reader.Value);

        // Display the attributes values for the current node
        if (reader.HasAttributes)
        {
            outputXML.Append(" Attributes:");

            for (int j=0; j < reader.AttributeCount; j++)
            {
                outputXML.Append(reader[j]);
            }
        }
        outputXML.Append("\r\n");
    }

    /// ************************* DOM *********************************
    protected void ShowDocument(XMLNode node)
    {
        if (node != null)
            Format (node);

        if (node.HasChildNodes)
        {
            node = node.FirstChild;
            while (node != null)
            {
                ShowDocument(node);
                node = node.NextSibling;
            }
        }
    }

    // Format the output
    private void Format (XMLNode node)
    {
        if (!node.HasChildNodes)
        {
           outputXML.Append("\t" + "<" + node.Value + ">");
        }

        else
        {
            outputXML.Append("<" + node.Name + ">");
            if (XMLNodeType.Element == node.NodeType)
            {
                XMLNamedNodeMap map = node.Attributes;
                foreach (XMLNode attrnode in map)
                    outputXML.Append(" " + attrnode.Name + "<" + attrnode.Value + "> ");
            }
            outputXML.Append("\r\n");
        }
    }
}


下面就是webform2.ASPx了
webform2.ASPx
                                ---
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.IO" %>
<%@ Assembly Name="System.XML" %>
<%@ Import Namespace="System.XML" %>
<%@ Page Language="C#" Inherits="WebForm2" Src="WebForm2.cs" Debug="true" %>

<Html><HEAD>

  <script runat="server" language="C#">
    // Put page script here
        public void On_KeyUp(object sender, System.EventArgs e)
    {
        Response.Write("Works");
    }

  </script>

<!--<link REL="STYLESHEET" HREF="default.CSS" TYPE="text/CSS">-->
<TITLE>test</TITLE>
</HEAD>

<BODY   >

   
<form method="post" action="WebForm2.ASPx" runat="server" enctype="multipart/form-data">
 
<div align="left">
<table>
  <tr>
    <td>XML Document:</td>
    <td><input type=file id="XMLFile" runat=server>         FileName:</td>
    <td><asp:label id="FileLabel" runat="server"></ASP:label></td>
  </tr>

  <tr>
    <td>XPath Expression</td>
    <td><ASP:textbox id=TextBox1 runat="server" Height="20" Width="300" text=".//text()"
OnKey_Up="On_KeyUp"></ASP:textbox></td>
    <td><ASP:button type=submit OnClick="Query_Click" runat="server" Height="20" Width="91"
text="Query"></ASP:button></td>
  </tr>
</table>

</br>
<table>
  <tr><td>Output from Query</td><td>XML Data</td><tr>
  <tr><td>Query Display: <ASP:dropdownlist runat="server">
                         <asp:listitem>Descriptive</ASP:listitem>
                         <asp:listitem>XML</ASP:listitem>
                         </ASP:dropdownlist>
  </td><tr>
  <tr>
    <td width="50%" valign="top" align="left"><ASP:textbox id=TextBox2 runat="server" Height="400"
Width="350" TextMode="MultiLine" Rows="10"></ASP:textbox></td>
    <td width="50%" valign="top" align="left"><ASP:xml id="MyXML" transformsource="test.xsl"
runat=server/></ASP:XML></td>
  </tr>
</table>
</div>

    <td><ASP:textbox id=TextBox3 runat="server" Height="1" Width="5" TextMode="MultiLine"
Rows="110"></ASP:textbox></td>

</form>
   
</BODY>
</Html>

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved