资源大全 | 神秘文化 | 在线翻译 | QQ专区 | 视频教程 | 彩信频道 | 搜索引擎 | BT下载 |  | 网站地图
设为首页
加入收藏
联系站长
您现在的位置: 一百网络 >> ASP.NET编程 >> XMLXSL >> 文档正文
最近更新
普通文档 用Asp.net实现基于XML的
普通文档 .Net中将图片数据保存到
普通文档 ASP.NET创建XML Web服务
普通文档 XML网络服务安全(1)
普通文档 HTML中的XML数据岛记录编
普通文档 XML文件导入SQL Server 
推荐文档 将Access数据转换为XML格
普通文档 不用编程,得到一个用户
普通文档 创建存储过程的XML注释文
普通文档 利用XSLT把ADO记录集转换
推荐文章
推荐文档 将Access数据转换为XML格
推荐文档 SQL Server和XML的集成
推荐文档 在DotNet里面利用XML
推荐文档 使用c#如何读取xml文件
在DotNet里面利用XML

文章作者:佚名 录入时间:2006-6-13 来源:不详
网站声明:本站的文章除部分特别声明禁止转载的专稿外,可以自由转载.但请务必注明出处和原始作者,文章版权归本网站与文章作者所有。对于被本站转载文章的个人和网站,我们表示深深的谢意。


呵呵,有意思,听过一个笑话,说德国一个老头,是个聋子,在如厕时,突然苏联战机空袭,虽然房屋倒塌,可是全家人都没有事情,家里人扒开卫生间的墙壁,发现老头在那儿乐,说:我一拉抽水马桶的绳,屋就倒了。
哈哈哈,我也是这样,刚才我一发这贴子,chinaasp就完了,还以为是我的事情呢,原来是苏联飞机来炸了,呵呵。
如果你喜欢玩xml,那么请跟我来,但在此之前,请深呼一口气,因为我给大家介绍一个长代码,在这个代码中揭示了微软在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>

  • 上一篇文档:

  • 下一篇文档:
  •     查找更多“在DotNet里面利用XML”的内容  
    相关连接
  • 用Asp.net实现基于XML的留言簿之一(1)

  • .Net中将图片数据保存到XML文档

  • ASP.NET创建XML Web服务全接触(1)

  • XML网络服务安全(1)

  • HTML中的XML数据岛记录编辑与添加