导 读:介绍了有关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())
{
&
在这个代码中揭示了微软在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())
{
&