ASP.NET中的XML表单控件
2024-07-10 12:57:57
供稿:网友
导 读:介绍了有关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>