导 读:介绍了有关ASP.NET中XML控件的使用,有个小BUG:在WEBFORM.ASPX中出现的XML控件,其中的transformsource属性设定了样式表文件路径,可是在文章出处没有找到这个XSL文件.:( 自己解决吧.
--------------------------------------------------------------------------------
在这个代码中揭示了微软在ASP.NET架构中隐藏的一个WEB表单控件,即
1<asp:xml runat="server/">,我只给代码,不给解释,大家自己下课后去研究吧。
2另外,由于是beta1,在这个控件中你使用的xslt里面不能使用<xsl:sort>,当然,亦不能使用那个order-by了,因为它支持的xsl空间是带"1999"的那个,而不是原来的那个。
3另外,我从微软得到的回答就是在beta2里面,它将支持<xsl:sort>,就可以全部转向xml+xsl了,而不用再为源代码保密问题头疼了。
4请看下例:
5webform2.cs
6\-
7using System;
8using System.Collections;
9using System.ComponentModel;
10using System.Data;
11using System.Drawing;
12using System.Web;
13using System.Web.SessionState;
14using System.Web.UI;
15using System.Web.UI.WebControls;
16using System.Web.UI.HtmlControls;
17using System.Text;
18using System.IO;
19using System.Xml;
20
21public class WebForm2 : Page
22{
23public StringBuilder outputQ;
24public StringBuilder outputXml;
25public DocumentNavigator nav = null;
26public HtmlInputFile XmlFile;
27
28public System.Web.UI.WebControls.Xml MyXml;
29
30public System.Web.UI.WebControls.TextBox TextBox1;
31public System.Web.UI.WebControls.TextBox TextBox2;
32public System.Web.UI.WebControls.TextBox TextBox3;
33public System.Web.UI.WebControls.Button Query;
34public System.Web.UI.WebControls.Label FileLabel;
35
36public void On_KeyUp(object sender, System.EventArgs e)
37{
38Response.Write("Works");
39}
40
41protected void Page_Load(object sender, EventArgs e)
42{
43if (!IsPostBack)
44{
45//
46// Evals true first time browser hits the page
47//
48}
49}
50
51public void Query_Click(object sender, System.EventArgs e)
52{
53HttpPostedFile xmlfile = XmlFile.PostedFile;
54XmlDocument doc = new XmlDocument();
55MyXml.Document = new XmlDocument();
56// TextBox2.Text="";
57// TextBox3.Text="";
58
59if (xmlfile.FileName != String.Empty)
60{
61try
62{
63FileLabel.Text= xmlfile.FileName;
64
65MyXml.Document.Load(xmlfile.FileName);
66outputXml = new StringBuilder();
67XmlTextReader reader = new XmlTextReader (xmlfile.FileName);
68ShowDocument();
69TextBox3.Text = outputXml.ToString();
70
71outputQ = new StringBuilder();
72doc.Load(xmlfile.FileName);
73DocumentNavigator nav = new DocumentNavigator(doc);
74// Perform the query e.g. "descendant::book/price"
75XPathQuery(nav, TextBox1.Text);
76TextBox2.Text = outputQ.ToString();
77
78}
79catch (Exception exp) {
80//outputQ.Append("<font color='\"#FF6600\"'>"+ exp.Message+"</font><xmp>");
81}
82finally {}
83}
84else if (FileLabel.Text != String.Empty)
85{
86try
87{
88MyXml.Document.Load(FileLabel.Text);
89outputXml = new StringBuilder();
90XmlTextReader reader = new XmlTextReader (FileLabel.Text);
91ShowDocument();
92TextBox3.Text = outputXml.ToString();
93
94ShowDocument();
95
96outputQ = new StringBuilder();
97doc.Load(FileLabel.Text);
98DocumentNavigator nav = new DocumentNavigator(doc);
99// Perform the query e.g. "descendant::book/price"
100XPathQuery(nav, TextBox1.Text);
101TextBox2.Text = outputQ.ToString();
102
103}
104catch (Exception exp) {
105outputQ.Append("</xmp><font color='\"#FF6600\"'>"+ exp.Message+"</font><xmp>");
106}
107finally {}
108}
109}
110
111private void XPathQuery(XmlNavigator navigator, String xpathexpr )
112{
113try
114{
115// Save context node position
116navigator.PushPosition();
117navigator.Select (xpathexpr);
118FormatXml(navigator);
119
120// Restore context node position
121navigator.PopPosition();
122}
123catch (Exception e)
124{
125}
126}
127
128//***************************** Navigator ************************************
129private void FormatXml (XmlNavigator navigator)
130{
131while (navigator.MoveToNextSelected())
132{
133switch (navigator.NodeType)
134{
135case XmlNodeType.ProcessingInstruction:
136Format (navigator, "ProcessingInstruction");
137break;
138case XmlNodeType.DocumentType:
139Format (navigator, "DocumentType");
140break;
141case XmlNodeType.Document:
142Format (navigator, "Document");
143break;
144case XmlNodeType.Comment:
145Format (navigator, "Comment");
146break;
147case XmlNodeType.Element:
148Format (navigator, "Element");
149break;
150case XmlNodeType.Text:
151Format (navigator, "Text");
152break;
153case XmlNodeType.Whitespace:
154Format (navigator, "Whitespace");
155break;
156}
157}
158outputQ.Append("\r\n");
159}
160
161// Format the output
162private void Format (XmlNavigator navigator, String NodeType)
163{
164String value = String.Empty;
165String name = String.Empty;
166
167if (navigator.HasChildren)
168{
169name = navigator.Name;
170navigator.MoveToFirstChild();
171if (navigator.HasValue)
172{
173value = navigator.Value;
174}
175}
176else
177{
178if (navigator.HasValue)
179{
180value = navigator.Value;
181name = navigator.Name;
182}
183}
184outputQ.Append(NodeType + "<" + name + ">" + value);
185outputQ.Append("\r\n");
186}
187
188// ********************************** XmlReader *****************************
189public void ShowDocument ()
190{
191outputXml = new StringBuilder();
192XmlTextReader reader = new XmlTextReader (FileLabel.Text);
193
194while (reader.Read())
195{
196switch (reader.NodeType)
197{
198case XmlNodeType.ProcessingInstruction:
199Format (reader, "ProcessingInstruction");
200break;
201case XmlNodeType.DocumentType:
202Format (reader, "DocumentType");
203break;
204case XmlNodeType.Comment:
205Format (reader, "Comment");
206break;
207case XmlNodeType.Element:
208Format (reader, "Element");
209break;
210case XmlNodeType.Text:
211Format (reader, "Text");
212break;
213case XmlNodeType.Whitespace:
214break;
215}
216}
217TextBox3.Text = outputXml.ToString();
218}
219
220protected void Format(XmlReader reader, String NodeType)
221{
222// Format the output
223for (int i=0; i < reader.Depth; i++)
224{
225outputXml.Append('\t');
226}
227
228outputXml.Append(reader.Prefix + NodeType + "<" + reader.Name + ">" + reader.Value);
229
230// Display the attributes values for the current node
231if (reader.HasAttributes)
232{
233outputXml.Append(" Attributes:");
234
235for (int j=0; j < reader.AttributeCount; j++)
236{
237outputXml.Append(reader[j]);
238}
239}
240outputXml.Append("\r\n");
241}
242
243/// ************************* DOM *********************************
244protected void ShowDocument(XmlNode node)
245{
246if (node != null)
247Format (node);
248
249if (node.HasChildNodes)
250{
251node = node.FirstChild;
252while (node != null)
253{
254ShowDocument(node);
255node = node.NextSibling;
256}
257}
258}
259
260// Format the output
261private void Format (XmlNode node)
262{
263if (!node.HasChildNodes)
264{
265outputXml.Append("\t" + "<" + node.Value + ">");
266}
267
268else
269{
270outputXml.Append("<" + node.Name + ">");
271if (XmlNodeType.Element == node.NodeType)
272{
273XmlNamedNodeMap map = node.Attributes;
274foreach (XmlNode attrnode in map)
275outputXml.Append(" " + attrnode.Name + "<" + attrnode.Value + "> ");
276}
277outputXml.Append("\r\n");
278}
279}
280}
281
282
283下面就是webform2.aspx了
284webform2.aspx
285\---
@ 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"
1
2<html><head>
3<script language="C#" runat="server">
4// Put page script here
5public void On_KeyUp(object sender, System.EventArgs e)
6{
7Response.Write("Works");
8}
9
10</script>
11<!--<link REL="STYLESHEET" HREF="default.css" TYPE="text/css">\-->
12<title>test</title>
13</head>
14<body>
15<form action="WebForm2.aspx" enctype="multipart/form-data" method="post" runat="server">
16<div align="left">
17<table>
18<tr>
19<td>XML Document:</td>
20<td><input id="XmlFile" runat="server" type="file"/> FileName:</td>
21<td><asp:label id="FileLabel" runat="server"></asp:label></td>
22</tr>
23<tr>
24<td>XPath Expression</td>
25<td><asp:textbox height="20" id="TextBox1" onkey_up="On_KeyUp" runat="server" text=".//text()" width="300"></asp:textbox></td>
26<td><asp:button height="20" onclick="Query_Click" runat="server" text="Query" type="submit" width="91"></asp:button></td>
27</tr>
28</table>
29
30<table>
31<tr><td>Output from Query</td><td>XML Data</td><tr>
32<tr><td>Query Display: <asp:dropdownlist runat="server">
33<asp:listitem>Descriptive</asp:listitem>
34<asp:listitem>XML</asp:listitem>
35</asp:dropdownlist>
36</td><tr>
37<tr>
38<td align="left" valign="top" width="50%"><asp:textbox height="400" id="TextBox2" rows="10" runat="server" textmode="MultiLine" width="350"></asp:textbox></td>
39<td align="left" valign="top" width="50%"><asp:xml id="MyXml" runat="server/" transformsource="test.xsl"></asp:xml></td>
40</tr>
41</tr></tr></tr></tr></table>
42</div>
43<td><asp:textbox height="1" id="TextBox3" rows="110" runat="server" textmode="MultiLine" width="5"></asp:textbox></td>
44</form>
45</body>
46</html></xmp></xsl:sort></xsl:sort></asp:xml>