阅读文章(首页/程序开发/.NET教程/)

ASP.NET中的XML表单控件

[日期:2004-06-25] 来源:  作者: [字体: ]

导 读:介绍了有关ASP.NET中XML控件的使用,有个小BUG:在WEBFORM.ASPX中出现的XML控件,其中的transformsource属性设定了样式表文件路径,可是在文章出处没有找到这个XSL文件.:( 自己解决吧.
--------------------------------------------------------------------------------
在这个代码中揭示了微软在ASP.NET架构中隐藏的一个WEB表单控件,即,我只给代码,不给解释,大家自己下课后去研究吧。
另外,由于是beta1,在这个控件中你使用的xslt里面不能使用,当然,亦不能使用那个order-by了,因为它支持的xsl空间是带"1999"的那个,而不是原来的那个。
另外,我从微软得到的回答就是在beta2里面,它将支持,就可以全部转向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(""+ exp.Message+"

");<BR>}<BR>finally {}<BR>}<BR>else if (FileLabel.Text != String.Empty)<BR>{<BR>try<BR>{<BR>My<a href="http://www.blue1000.com/bkhtml/c21/" title="XML教程">XML</a>.Document.Load(FileLabel.Text);<BR>output<a href="http://www.blue1000.com/bkhtml/c21/" title="XML教程">XML</a> = new StringBuilder();<BR><a href="http://www.blue1000.com/bkhtml/c21/" title="XML教程">XML</a>TextReader reader = new <a href="http://www.blue1000.com/bkhtml/c21/" title="XML教程">XML</a>TextReader (FileLabel.Text);<BR>ShowDocument();<BR>TextBox3.Text = output<a href="http://www.blue1000.com/bkhtml/c21/" title="XML教程">XML</a>.ToString();</P> <P>ShowDocument();</P> <P>outputQ = new StringBuilder();<BR>doc.Load(FileLabel.Text);<BR>DocumentNavigator nav = new DocumentNavigator(doc);<BR>// Perform the query e.g. "descendant::book/price"<BR>XPathQuery(nav, TextBox1.Text);<BR>TextBox2.Text = outputQ.ToString();</P> <P>}<BR>catch (Exception exp) {<BR>outputQ.Append(""+ exp.Message+"");<BR>}<BR>finally {}<BR>}<BR>}</P> <P>private void XPathQuery(<a href="http://www.blue1000.com/bkhtml/c21/" title="XML教程">XML</a>Navigator navigator, String xpathexpr )<BR>{<BR>try<BR>{<BR>// Save context node position<BR>navigator.PushPosition();<BR>navigator.Select (xpathexpr);<BR>Format<a href="http://www.blue1000.com/bkhtml/c21/" title="XML教程">XML</a>(navigator);</P> <P>// Restore context node position<BR>navigator.PopPosition(); <BR>}<BR>catch (Exception e)<BR>{<BR>}<BR>}<BR><BR>//***************************** Navigator ************************************<BR>private void Format<a href="http://www.blue1000.com/bkhtml/c21/" title="XML教程">XML</a> (<a href="http://www.blue1000.com/bkhtml/c21/" title="XML教程">XML</a>Navigator navigator)<BR>{<BR>while (navigator.MoveToNextSelected())<BR>{<BR>switch (navigator.NodeType)<BR>{<BR>case <a href="http://www.blue1000.com/bkhtml/c21/" title="XML教程">XML</a>NodeType.ProcessingInstruction:<BR>Format (navigator, "ProcessingInstruction");<BR>break;<BR>case <a href="http://www.blue1000.com/bkhtml/c21/" title="XML教程">XML</a>NodeType.DocumentType:<BR>Format (navigator, "DocumentType");<BR>break;<BR>case <a href="http://www.blue1000.com/bkhtml/c21/" title="XML教程">XML</a>NodeType.Document:<BR>Format (navigator, "Document");<BR>break;<BR>case <a href="http://www.blue1000.com/bkhtml/c21/" title="XML教程">XML</a>NodeType.Comment:<BR>Format (navigator, "Comment");<BR>break;<BR>case <a href="http://www.blue1000.com/bkhtml/c21/" title="XML教程">XML</a>NodeType.Element:<BR>Format (navigator, "Element");<BR>break;<BR>case <a href="http://www.blue1000.com/bkhtml/c21/" title="XML教程">XML</a>NodeType.Text:<BR>Format (navigator, "Text");<BR>break;<BR>case <a href="http://www.blue1000.com/bkhtml/c21/" title="XML教程">XML</a>NodeType.Whitespace:<BR>Format (navigator, "Whitespace");<BR>break;<BR>}<BR>}<BR>outputQ.Append("\r\n");<BR>}</P> <P>// Format the output<BR>private void Format (<a href="http://www.blue1000.com/bkhtml/c21/" title="XML教程">XML</a>Navigator navigator, String NodeType)<BR>{<BR>String value = String.Empty;<BR>String name = String.Empty;</P> <P>if (navigator.HasChildren)<BR>{<BR>name = navigator.Name;<BR>navigator.MoveToFirstChild();<BR>if (navigator.HasValue)<BR>{<BR>value = navigator.Value;<BR>}<BR>}<BR>else<BR>{<BR>if (navigator.HasValue)<BR>{<BR>value = navigator.Value;<BR>name = navigator.Name;<BR>}<BR>}<BR>outputQ.Append(NodeType + "<" + name + ">" + value);<BR>outputQ.Append("\r\n");<BR>}</P> <P>// ********************************** <a href="http://www.blue1000.com/bkhtml/c21/" title="XML教程">XML</a>Reader *****************************<BR>public void ShowDocument ()<BR>{<BR>output<a href="http://www.blue1000.com/bkhtml/c21/" title="XML教程">XML</a> = new StringBuilder();<BR><a href="http://www.blue1000.com/bkhtml/c21/" title="XML教程">XML</a>TextReader reader = new <a href="http://www.blue1000.com/bkhtml/c21/" title="XML教程">XML</a>TextReader (FileLabel.Text);</P> <P>while (reader.Read())<BR>{<BR>switch (reader.NodeType)<BR>{<BR>case <a href="http://www.blue1000.com/bkhtml/c21/" title="XML教程">XML</a>NodeType.ProcessingInstruction:<BR>Format (reader, "ProcessingInstruction");<BR>break;<BR>case <a href="http://www.blue1000.com/bkhtml/c21/" title="XML教程">XML</a>NodeType.DocumentType:<BR>Format (reader, "DocumentType");<BR>break;<BR>case <a href="http://www.blue1000.com/bkhtml/c21/" title="XML教程">XML</a>NodeType.Comment:<BR>Format (reader, "Comment");<BR>break;<BR>case <a href="http://www.blue1000.com/bkhtml/c21/" title="XML教程">XML</a>NodeType.Element:<BR>Format (reader, "Element");<BR>break;<BR>case <a href="http://www.blue1000.com/bkhtml/c21/" title="XML教程">XML</a>NodeType.Text:<BR>Format (reader, "Text");<BR>break;<BR>case <a href="http://www.blue1000.com/bkhtml/c21/" title="XML教程">XML</a>NodeType.Whitespace:<BR>break;<BR>}<BR>}<BR>TextBox3.Text = output<a href="http://www.blue1000.com/bkhtml/c21/" title="XML教程">XML</a>.ToString();<BR>}</P> <P>protected void Format(<a href="http://www.blue1000.com/bkhtml/c21/" title="XML教程">XML</a>Reader reader, String NodeType)<BR>{<BR>// Format the output<BR>for (int i=0; i < reader.Depth; i++)<BR>{ <BR>output<a href="http://www.blue1000.com/bkhtml/c21/" title="XML教程">XML</a>.Append('\t');<BR>}</P> <P>output<a href="http://www.blue1000.com/bkhtml/c21/" title="XML教程">XML</a>.Append(reader.Prefix + NodeType + "<" + reader.Name + ">" + reader.Value);</P> <P>// Display the attributes values for the current node<BR>if (reader.HasAttributes)<BR>{<BR>output<a href="http://www.blue1000.com/bkhtml/c21/" title="XML教程">XML</a>.Append(" Attributes:");</P> <P>for (int j=0; j < reader.AttributeCount; j++)<BR>{<BR>output<a href="http://www.blue1000.com/bkhtml/c21/" title="XML教程">XML</a>.Append(reader[j]);<BR>}<BR>}<BR>output<a href="http://www.blue1000.com/bkhtml/c21/" title="XML教程">XML</a>.Append("\r\n");<BR>}</P> <P>/// ************************* DOM *********************************<BR>protected void ShowDocument(<a href="http://www.blue1000.com/bkhtml/c21/" title="XML教程">XML</a>Node node)<BR>{<BR>if (node != null)<BR>Format (node);</P> <P>if (node.HasChildNodes)<BR>{<BR>node = node.FirstChild;<BR>while (node != null)<BR>{<BR>ShowDocument(node);<BR>node = node.NextSibling;<BR>}<BR>}<BR>}</P> <P>// Format the output<BR>private void Format (<a href="http://www.blue1000.com/bkhtml/c21/" title="XML教程">XML</a>Node node)<BR>{<BR>if (!node.HasChildNodes)<BR>{<BR>output<a href="http://www.blue1000.com/bkhtml/c21/" title="XML教程">XML</a>.Append("\t" + "<" + node.Value + ">");<BR>}</P> <P>else<BR>{<BR>output<a href="http://www.blue1000.com/bkhtml/c21/" title="XML教程">XML</a>.Append("<" + node.Name + ">");<BR>if (<a href="http://www.blue1000.com/bkhtml/c21/" title="XML教程">XML</a>NodeType.Element == node.NodeType)<BR>{<BR><a href="http://www.blue1000.com/bkhtml/c21/" title="XML教程">XML</a>NamedNodeMap map = node.Attributes;<BR>foreach (<a href="http://www.blue1000.com/bkhtml/c21/" title="XML教程">XML</a>Node attrnode in map)<BR>output<a href="http://www.blue1000.com/bkhtml/c21/" title="XML教程">XML</a>.Append(" " + attrnode.Name + "<" + attrnode.Value + "> ");<BR>}<BR>output<a href="http://www.blue1000.com/bkhtml/c21/" title="XML教程">XML</a>.Append("\r\n");<BR>}<BR>}<BR>}</P> <P><BR>下面就是webform2.<a href="http://www.blue1000.com/bkhtml/c16/" title="ASP教程">ASP</a>x了<BR>webform2.<a href="http://www.blue1000.com/bkhtml/c16/" title="ASP教程">ASP</a>x<BR>---<BR><%@ Import Namespace="System" %><BR><%@ Import Namespace="System.IO" %><BR><%@ Assembly Name="System.Xml" %><BR><%@ Import Namespace="System.Xml" %><BR><%@ Page Language="C#" Inherits="WebForm2" Src="http://www.blue1000.com/article/WebForm2.cs" Debug="true" %></P> <P><HTML><HEAD></P> <P><script runat="server" language="C#"><BR>// Put page script here<BR>public void On_KeyUp(object sender, System.EventArgs e)<BR>{<BR>Response.Write("Works");<BR>}</P> <P></script></P> <P><!--<link REL="STYLESHEET" HREF="http://www.blue1000.com/article/default.css" TYPE="text/css">--><BR><TITLE>test</TITLE><BR></HEAD></P> <P><BODY ></P> <P><BR><form method="post" action="WebForm2.aspx" runat="server" enctype="multipart/form-data"><BR><BR><div align="left"><BR><table><BR><tr><BR><td><a href="http://www.blue1000.com/bkhtml/c21/" title="XML教程">XML</a> Document:</td><BR><td><input type=file id="XmlFile" runat=server> FileName:</td><BR><td><asp:label id="FileLabel" runat="server"></asp:label></td><BR></tr></P> <P><tr><BR><td>XPath Expression</td><BR><td><asp:textbox id=TextBox1 runat="server" Height="20" Width="300" text=".//text()" OnKey_Up="On_KeyUp"></asp:textbox></td><BR><td><asp:button type=submit OnClick="Query_Click" runat="server" Height="20" Width="91" text="Query"></asp:button></td><BR></tr><BR></table></P> <P></br><BR><table><BR><tr><td>Output from Query</td><td><a href="http://www.blue1000.com/bkhtml/c21/" title="XML教程">XML</a> Data</td><tr><BR><tr><td>Query Display: <asp:dropdownlist runat="server"><BR><asp:listitem>Descriptive</asp:listitem><BR><asp:listitem><a href="http://www.blue1000.com/bkhtml/c21/" title="XML教程">XML</a></asp:listitem><BR></asp:dropdownlist><BR></td><tr><BR><tr><BR><td width="50%" valign="top" align="left"><asp:textbox id=TextBox2 runat="server" Height="400" Width="350" TextMode="MultiLine" Rows="10"></asp:textbox></td><BR><td width="50%" valign="top" align="left"><asp:xml id="MyXml" transformsource="test.xsl" runat=server/></asp:xml></td><BR></tr><BR></table><BR></div></P> <P><td><asp:textbox id=TextBox3 runat="server" Height="1" Width="5" TextMode="MultiLine" Rows="110"></asp:textbox></td></P> <P></form><BR><BR></BODY><BR></HTML></P> </div><br/><br/> </div> <div align="center"> <script type="text/javascript"><!-- google_ad_client = "pub-9733229352463134"; /* 728x15, 创建于 08-8-27 */ google_ad_slot = "6882425645"; google_ad_width = 728; google_ad_height = 15; //--> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script> </div> <table width="97%" cellpadding="0" cellspacing="0" style="clear:both"> <tr><td align="right"> <table><tr> <td> 阅读:<span id="news_hits"></span> 次<br/> 录入:<span id="MemberNameLabel"></span><br/><br/> </td></tr> </table> </td></tr> <tr><td align="right"> 【 <a href="../../remark.aspx?ID=4085" target="_blank">评论</a> 】 【 <a href="../../mail.aspx?ID=4085" target="_blank">推荐</a> 】 【 <a href="javascript:doPrint()">打印</a> 】 </td></tr> </table> <table width="97%" align="center" style="clear:both"> <tr><td> 上一篇:<a href="../../bkhtml/2004-06/4084.htm">ASP.NET实现HTTP方式获取功能</a><br/> 下一篇:<a href="../../bkhtml/2004-06/4086.htm">Microsoft .NET 框架常见问题 (二)</a> </td></tr> </table> </td> <td class="mr"></td> </tr> </table> </div> </div> <div class="mframe"> <table width="100%" cellspacing="0" cellpadding="0"> <tr> <td class="tl"></td> <td class="tm"> <span class="tt">相关文章</span> &nbsp; &nbsp; &nbsp; </td> <td class="tr"></td> </tr> </table> <table width="100%" cellspacing="0" cellpadding="0"> <tr> <td class="ml"></td> <td class="mm"> </td> <td class="mr"></td> </tr> </table> </div> <div class="mframe"> <table width="100%" align="center" cellspacing="0" cellpadding="0"> <tr> <td class="tl"></td> <td class="tm"> <a href="../../remark.aspx?id=4085" target="_blank" style="cursor:hand"><span class="tt">本文评论</span></a> </td> <td class="tr"></td> </tr> </table> <table width="100%" align="center" cellspacing="0" cellpadding="0"> <tr> <td class="ml"></td> <td class="mm"> </td> <td class="mr"></td> </tr> </table> </div> <div class="mframe"> <table width="100%" align="center" cellspacing="0" cellpadding="0"> <tr> <td class="tl"></td> <td class="tm"> <span class="tt">发表评论</span> </td> <td class="tr"></td> </tr> </table> <table width="100%" align="center" cellspacing="0" cellpadding="0"> <tr> <td class="ml"></td> <td class="mm"> <form id="remarkForm" action="../../remark.aspx?id=4085" method="post" onsubmit="return checkRemark();"> <table width="100%" cellpadding="5" cellspacing="0" border="0"> <tr><td> <script type="text/javascript" language="javascript"> <!-- function checkRemark() { var form=document.getElementById("remarkForm"); var remarkSize = 200; if (form.body.value=="") { alert("请填写评论内容"); form.body.focus(); return false; } if (form.username.value=="") { alert("请填写姓名"); form.username.focus(); return false; } if (form.body.value.length>remarkSize) { form.body.value = form.body.value.substr(0,remarkSize); showLen(form.body); form.body.focus(); alert("评论内容不可以超过"+remarkSize+"字,已帮你删除多余部分"); return false; } if (form.username.value.length>10) { alert("姓名不可以超过10个字"); form.username.focus(); return false; } form.submit.disabled=true; form.vcode.value = VCode("ASP.NET中的XML表单控件"); return true; } function showLen(obj) { document.getElementById("bodyLen").value=obj.value.length; } --> </script> <input type="radio" name="face" value="1" checked="checked"/><img src="../../pic/face1.gif" alt=""/> <input type="radio" name="face" value="2"/><img src="../../pic/face2.gif" alt=""/> <input type="radio" name="face" value="3"/><img src="../../pic/face3.gif" alt=""/> <input type="radio" name="face" value="4"/><img src="../../pic/face4.gif" alt=""/> <input type="radio" name="face" value="5"/><img src="../../pic/face5.gif" alt=""/> <input type="radio" name="face" value="6"/><img src="../../pic/face6.gif" alt=""/> <input type="radio" name="face" value="7"/><img src="../../pic/face7.gif" alt=""/> <input type="radio" name="face" value="8"/><img src="../../pic/face8.gif" alt=""/> <input type="radio" name="face" value="9"/><img src="../../pic/face9.gif" alt=""/><br/> <input type="radio" name="face" value="10"/><img src="../../pic/face10.gif" alt=""/> <input type="radio" name="face" value="11"/><img src="../../pic/face11.gif" alt=""/> <input type="radio" name="face" value="12"/><img src="../../pic/face12.gif" alt=""/> <input type="radio" name="face" value="13"/><img src="../../pic/face13.gif" alt=""/> <input type="radio" name="face" value="14"/><img src="../../pic/face14.gif" alt=""/> <input type="radio" name="face" value="15"/><img src="../../pic/face15.gif" alt=""/> <input type="radio" name="face" value="16"/><img src="../../pic/face16.gif" alt=""/> <input type="radio" name="face" value="17"/><img src="../../pic/face17.gif" alt=""/> <input type="radio" name="face" value="18"/><img src="../../pic/face18.gif" alt=""/><br/> 点评: <textarea name="body" cols="40" rows="4" onkeydown="showLen(this)" onkeyup="showLen(this)"></textarea> 字数<input type="text" id="bodyLen" size="4" style="border-width:0;background:transparent;"/> <br/> 姓名: <input type="text" id="i_username" name="username" value="" maxlength="15" size="10" /> <input type="submit" name="submit" id="i_submit" value=" 发 表 "/> <script type="text/javascript"> var remarkmember = false; var allowremark = true; if (remarkmember){ document.getElementById("i_username").readonly=true; document.write("(限会员登陆后发表评论)"); } if (!allowremark){ document.getElementById("i_submit").disabled=true; } </script> <br/><br/> <script type="text/javascript" src="../../inc/clientDate.js"></script> <input type="hidden" name="vcode" value=""/> </td> <td width="350"> <ul style="list-style-type:square;margin-left:1em;line-height:150%"> <li>尊重网上道德,遵守中华人民共和国的各项有关法律法规</li> <li>承担一切因您的行为而直接或间接导致的民事或刑事法律责任</li> <li>本站管理人员有权保留或删除其管辖留言中的任意内容</li> <li>本站有权在网站内转载或引用您的评论</li> <li>参与本评论即表明您已经阅读并接受上述条款</li> </ul> </td></tr> </table> </form> </td> <td class="mr"></td> </tr> </table> </div> <iframe src="../../frm_hit.aspx?id=4085&disp=1" border="0" height="0" width="0" style="visibility: hidden"></iframe> <table width="100%"><tr> <td class="hr"></td> </tr></table> <table id="footer" border="0" cellpadding="0" cellspacing="0"> <tr align="center" style="line-height:130%"><td height="60"> <BR><IMG style="FLOAT: left" src="/style/blue1000com/bkbz.gif" border=0><FONT color=#005c96><B>BK网络学院</B>主要内容:<STRONG>平面设计教程,网站开发在线教程,网页制作教程,服务器教程